├── packages ├── jest │ ├── src │ │ ├── modules │ │ │ ├── index.ts │ │ │ └── prependImport.ts │ │ ├── __fixtures__ │ │ │ ├── mock │ │ │ │ ├── enableAutomock.error.js │ │ │ │ ├── __mocks__ │ │ │ │ │ └── getToken.js │ │ │ │ ├── contexts.input.js │ │ │ │ ├── defaultExport.js │ │ │ │ ├── getToken.js │ │ │ │ ├── withImplementation.input.js │ │ │ │ ├── getTokenModule.js │ │ │ │ ├── contexts.output.js │ │ │ │ ├── isMockFunction.input.js │ │ │ │ ├── withImplementation.output.js │ │ │ │ ├── requireActual.input.js │ │ │ │ ├── mockName.input.js │ │ │ │ ├── requireMock.input.js │ │ │ │ ├── unmock.input.js │ │ │ │ ├── getMockName.input.js │ │ │ │ ├── isMockFunction.output.js │ │ │ │ ├── deepUnmock.input.js │ │ │ │ ├── disableAutomock.input.js │ │ │ │ ├── genMockFromModule.input.js │ │ │ │ ├── mock-named.input.js │ │ │ │ ├── createMockFromModule.input.js │ │ │ │ ├── disableAutomock.output.js │ │ │ │ ├── mockName.output.js │ │ │ │ ├── requireActual.output.js │ │ │ │ ├── fn.input.js │ │ │ │ ├── requireMock.output.js │ │ │ │ ├── unmock.output.js │ │ │ │ ├── getMockName.output.js │ │ │ │ ├── deepUnmock.output.js │ │ │ │ ├── mock-named.output.js │ │ │ │ ├── genMockFromModule.output.js │ │ │ │ ├── setMock-default.input.js │ │ │ │ ├── mockImplementation.input.js │ │ │ │ ├── createMockFromModule.output.js │ │ │ │ ├── setMock-named.input.js │ │ │ │ ├── mock-default-arrow-fn.input.js │ │ │ │ ├── fn.output.js │ │ │ │ ├── mockImplementationOnce.input.js │ │ │ │ ├── mock-factory-named.input.js │ │ │ │ ├── mock-default-fn-expr.input.js │ │ │ │ ├── mockImplementation.output.js │ │ │ │ ├── setMock-named.output.js │ │ │ │ ├── mock-factory-named.output.js │ │ │ │ ├── mockImplementationOnce.output.js │ │ │ │ ├── setMock-default.output.js │ │ │ │ ├── mock-default-arrow-fn.output.js │ │ │ │ ├── mock-default-fn-expr.output.js │ │ │ │ ├── instances.input.js │ │ │ │ ├── resetModules.input.js │ │ │ │ ├── instances.output.js │ │ │ │ ├── resetModules.output.js │ │ │ │ ├── mockRestore.input.js │ │ │ │ ├── mockRestore.output.js │ │ │ │ ├── mockReturnThis.input.js │ │ │ │ ├── mockReturnThis.output.js │ │ │ │ ├── mockClear.input.js │ │ │ │ ├── doMock.input.js │ │ │ │ ├── mockClear.output.js │ │ │ │ ├── doMock.output.js │ │ │ │ ├── mockReset.input.js │ │ │ │ ├── spyOn.input.js │ │ │ │ ├── mockReset.output.js │ │ │ │ └── spyOn.output.js │ │ │ ├── expect │ │ │ │ ├── toBe.input.js │ │ │ │ ├── not.input.js │ │ │ │ ├── toBeNaN.input.js │ │ │ │ ├── toBeNull.input.js │ │ │ │ ├── toBeDefined.input.js │ │ │ │ ├── toBeLessThan.input.js │ │ │ │ ├── toMatch.input.js │ │ │ │ ├── toBeGreaterThan.input.js │ │ │ │ ├── toBeUndefined.input.js │ │ │ │ ├── toContain.input.js │ │ │ │ ├── not.output.js │ │ │ │ ├── toBe.output.js │ │ │ │ ├── toBeNaN.output.js │ │ │ │ ├── hasAssertions.input.js │ │ │ │ ├── toBeNull.output.js │ │ │ │ ├── toContainEqual.input.js │ │ │ │ ├── toBeDefined.output.js │ │ │ │ ├── toBeFalsy.input.js │ │ │ │ ├── toBeLessThan.output.js │ │ │ │ ├── toBeTruthy.input.js │ │ │ │ ├── toHaveLength.input.js │ │ │ │ ├── toMatch.output.js │ │ │ │ ├── toBeGreaterThan.output.js │ │ │ │ ├── toBeUndefined.output.js │ │ │ │ ├── toContain.output.js │ │ │ │ ├── toBeLessThanOrEqual.input.js │ │ │ │ ├── toMatchObject.input.js │ │ │ │ ├── arrayContaining.input.js │ │ │ │ ├── toBeGreaterThanOrEqual.input.js │ │ │ │ ├── hasAssertions.output.js │ │ │ │ ├── toBeInstanceOf.input.js │ │ │ │ ├── toContainEqual.output.js │ │ │ │ ├── stringMatching.input.js │ │ │ │ ├── toBeFalsy.output.js │ │ │ │ ├── stringContaining.input.js │ │ │ │ ├── toBeTruthy.output.js │ │ │ │ ├── toHaveLength.output.js │ │ │ │ ├── toBeLessThanOrEqual.output.js │ │ │ │ ├── toMatchObject.output.js │ │ │ │ ├── arrayContaining.output.js │ │ │ │ ├── toBeGreaterThanOrEqual.output.js │ │ │ │ ├── toBeInstanceOf.output.js │ │ │ │ ├── stringMatching.output.js │ │ │ │ ├── stringContaining.output.js │ │ │ │ ├── objectContaining.input.js │ │ │ │ ├── toStrictEqual.input.js │ │ │ │ ├── objectContaining.output.js │ │ │ │ ├── toStrictEqual.output.js │ │ │ │ ├── assertions.input.js │ │ │ │ ├── toBeCloseTo.input.js │ │ │ │ ├── assertions.output.js │ │ │ │ ├── toBeCloseTo.output.js │ │ │ │ ├── toThrow.input.js │ │ │ │ ├── toThrowError.input.js │ │ │ │ ├── toThrow.output.js │ │ │ │ ├── toThrowError.output.js │ │ │ │ ├── toHaveProperty.input.js │ │ │ │ └── toHaveProperty.output.js │ │ │ ├── test │ │ │ │ ├── test-todo.input.js │ │ │ │ ├── fit.input.js │ │ │ │ ├── fit-fails.input.js │ │ │ │ ├── test-todo.output.js │ │ │ │ ├── fit.output.js │ │ │ │ ├── fit-fails.output.js │ │ │ │ ├── fit-each.input.js │ │ │ │ ├── test-only.input.js │ │ │ │ ├── test-skip.input.js │ │ │ │ ├── test-fails.input.js │ │ │ │ ├── test-concurrent.input.js │ │ │ │ ├── test-only-fails.input.js │ │ │ │ ├── test-skip-fails.input.js │ │ │ │ ├── fit-each.output.js │ │ │ │ ├── test-only.output.js │ │ │ │ ├── test-skip.output.js │ │ │ │ ├── test-fails.output.js │ │ │ │ ├── test-only-fails.output.js │ │ │ │ ├── test-skip-fails.output.js │ │ │ │ ├── test-concurrent.output.js │ │ │ │ ├── test-each.input.js │ │ │ │ ├── test-only-each.input.js │ │ │ │ ├── test-skip-each.input.js │ │ │ │ ├── test-fails-each.input.js │ │ │ │ ├── test-concurrent-each.input.js │ │ │ │ ├── test-concurrent-only-each.input.js │ │ │ │ ├── test-concurrent-skip-each.input.js │ │ │ │ ├── test-each.output.js │ │ │ │ ├── test-only-each.output.js │ │ │ │ ├── test-skip-each.output.js │ │ │ │ ├── test-fails-each.output.js │ │ │ │ ├── test-concurrent-each.output.js │ │ │ │ ├── test-concurrent-only-each.output.js │ │ │ │ └── test-concurrent-skip-each.output.js │ │ │ ├── describe │ │ │ │ ├── describe-skip.input.js │ │ │ │ ├── describe-skip.output.js │ │ │ │ ├── decribe-each.input.js │ │ │ │ ├── describe-skip-each.input.js │ │ │ │ ├── describe-only.input.js │ │ │ │ ├── decribe-each.output.js │ │ │ │ ├── describe-skip-each.output.js │ │ │ │ ├── describe-only.output.js │ │ │ │ ├── describe-only-each.input.js │ │ │ │ └── describe-only-each.output.js │ │ │ ├── calls │ │ │ │ ├── toBeCalled.input.js │ │ │ │ ├── toHaveBeenCalled.input.js │ │ │ │ ├── toBeCalled.output.js │ │ │ │ ├── toHaveBeenCalled.output.js │ │ │ │ ├── toBeCalledTimes.input.js │ │ │ │ ├── toHaveBeenCalledTimes.input.js │ │ │ │ ├── toBeCalledTimes.output.js │ │ │ │ ├── lastCall.input.js │ │ │ │ ├── lastCalledWith.input.js │ │ │ │ ├── toBeCalledWith.input.js │ │ │ │ ├── nthCalledWith.input.js │ │ │ │ ├── toHaveBeenCalledTimes.output.js │ │ │ │ ├── toHaveBeenCalledWith.input.js │ │ │ │ ├── lastCall.output.js │ │ │ │ ├── toBeCalledWith.output.js │ │ │ │ ├── lastCalledWith.output.js │ │ │ │ ├── nthCalledWith.output.js │ │ │ │ ├── toHaveBeenLastCalledWith.input.js │ │ │ │ ├── toHaveBeenNthCalledWith.input.js │ │ │ │ ├── toHaveBeenCalledWith.output.js │ │ │ │ ├── calls.input.js │ │ │ │ ├── toHaveBeenLastCalledWith.output.js │ │ │ │ ├── toHaveBeenNthCalledWith.output.js │ │ │ │ └── calls.output.js │ │ │ ├── snapshot │ │ │ │ ├── toThrowErrorMatchingInlineSnapshot.input.js │ │ │ │ ├── __snapshots__ │ │ │ │ │ ├── toThrowErrorMatchingSnapshot.output.js.snap │ │ │ │ │ ├── toThrowErrorMatchingSnapshot.input.js.snap │ │ │ │ │ ├── addSnapshotSerializer.output.js.snap │ │ │ │ │ ├── addSnapshotSerializer.input.js.snap │ │ │ │ │ ├── toMatchSnapshot.output.js.snap │ │ │ │ │ └── toMatchSnapshot.input.js.snap │ │ │ │ ├── toThrowErrorMatchingInlineSnapshot.output.js │ │ │ │ ├── toThrowErrorMatchingSnapshot.input.js │ │ │ │ ├── toMatchInlineSnapshot.input.js │ │ │ │ ├── toThrowErrorMatchingSnapshot.output.js │ │ │ │ ├── toMatchInlineSnapshot.output.js │ │ │ │ ├── toMatchSnapshot.input.js │ │ │ │ ├── toMatchSnapshot.output.js │ │ │ │ ├── addSnapshotSerializer.input.js │ │ │ │ └── addSnapshotSerializer.output.js │ │ │ ├── misc │ │ │ │ ├── basic.input.js │ │ │ │ ├── basic.output.js │ │ │ │ ├── with-top-line-comment.input.js │ │ │ │ ├── replace-jest-imports.output.mjs │ │ │ │ ├── replace-jest-imports.input.mjs │ │ │ │ ├── with-file-headers.input.js │ │ │ │ ├── with-top-line-comment.output.js │ │ │ │ ├── with-existing-imports.input.mjs │ │ │ │ ├── with-file-headers.output.js │ │ │ │ ├── with-existing-imports.output.mjs │ │ │ │ ├── with-top-jsdoc-comment.input.js │ │ │ │ └── with-top-jsdoc-comment.output.js │ │ │ ├── returns │ │ │ │ ├── mockReturnValue.input.js │ │ │ │ ├── mockReturnValueOnce.input.js │ │ │ │ ├── toReturnWith.input.js │ │ │ │ ├── lastReturnedWith.input.js │ │ │ │ ├── nthReturnedWith.input.js │ │ │ │ ├── toHaveReturnedWith.input.js │ │ │ │ ├── mockReturnValue.output.js │ │ │ │ ├── mockResolvedValue.input.js │ │ │ │ ├── toHaveLastReturnedWith.input.js │ │ │ │ ├── mockResolvedValueOnce.input.js │ │ │ │ ├── toHaveNthReturnedWith.input.js │ │ │ │ ├── toReturnWith.output.js │ │ │ │ ├── mockReturnValueOnce.output.js │ │ │ │ ├── lastReturnedWith.output.js │ │ │ │ ├── nthReturnedWith.output.js │ │ │ │ ├── toHaveReturnedWith.output.js │ │ │ │ ├── mockResolvedValue.output.js │ │ │ │ ├── toHaveLastReturnedWith.output.js │ │ │ │ ├── toHaveNthReturnedWith.output.js │ │ │ │ ├── toReturn.input.js │ │ │ │ ├── mockResolvedValueOnce.output.js │ │ │ │ ├── toHaveReturned.input.js │ │ │ │ ├── toReturn.output.js │ │ │ │ ├── toHaveReturned.output.js │ │ │ │ ├── toReturnTimes.input.js │ │ │ │ ├── toHaveReturnedTimes.input.js │ │ │ │ ├── mockRejectedValue.input.js │ │ │ │ ├── toReturnTimes.output.js │ │ │ │ ├── mockRejectedValueOnce.input.js │ │ │ │ ├── toHaveReturnedTimes.output.js │ │ │ │ ├── mockRejectedValue.output.js │ │ │ │ ├── mockRejectedValueOnce.output.js │ │ │ │ ├── results.input.js │ │ │ │ └── results.output.js │ │ │ └── async │ │ │ │ ├── resolves.input.js │ │ │ │ ├── rejects.input.js │ │ │ │ ├── resolves.output.js │ │ │ │ ├── rejects.output.js │ │ │ │ ├── await.input.js │ │ │ │ ├── await.output.js │ │ │ │ ├── promises.input.js │ │ │ │ └── promises.output.js │ │ ├── snapshots │ │ │ ├── index.ts │ │ │ └── getSnapshotWithoutPrototype.ts │ │ ├── index.ts │ │ ├── apis │ │ │ ├── index.ts │ │ │ ├── getApisFromCallExpression.ts │ │ │ ├── addFactoryFunctionToMock.ts │ │ │ ├── replaceTestApiFit.ts │ │ │ ├── replaceTestApiFailing.ts │ │ │ ├── getApisFromMemberExpression.ts │ │ │ ├── replaceJestObjectWithVi.ts │ │ │ └── updateDefaultExportMocks.ts │ │ ├── transformer.ts │ │ └── transformer.spec.ts │ ├── jest.config.json │ ├── tsconfig.build.json │ ├── README.md │ ├── package.json │ └── CHANGELOG.md ├── vitest-codemod │ ├── bin │ │ └── vitest-codemod │ ├── src │ │ ├── utils │ │ │ ├── getTransforms.ts │ │ │ ├── getUpdatedTransformFile.ts │ │ │ ├── index.ts │ │ │ ├── getHelpParagraph.ts │ │ │ ├── getTransformDescription.ts │ │ │ └── getJsCodeshiftParser.ts │ │ └── cli.ts │ ├── tsconfig.build.json │ ├── package.json │ ├── README.md │ └── CHANGELOG.md └── types │ ├── tsconfig.build.json │ ├── README.md │ ├── package.json │ ├── CHANGELOG.md │ └── src │ └── index.ts ├── .eslintrc ├── .gitattributes ├── .vscode └── settings.json ├── .github ├── workflows │ ├── pull_request.yml │ ├── build.yml │ ├── push.yml │ └── lock.yml ├── PULL_REQUEST_TEMPLATE.md └── ISSUE_TEMPLATE │ ├── feature_request.yml │ ├── feature_request_transform.yml │ ├── bug_report.yml │ └── bug_report_transform.yml ├── tsconfig.build.json ├── tsconfig.json ├── .yarnrc.yml ├── vitest.config.ts ├── .changeset └── config.json ├── README.md ├── LICENSE ├── package.json └── .gitignore /packages/jest/src/modules/index.ts: -------------------------------------------------------------------------------- 1 | export * from './prependImport' 2 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/enableAutomock.error.js: -------------------------------------------------------------------------------- 1 | jest.enableAutomock(); -------------------------------------------------------------------------------- /packages/jest/src/snapshots/index.ts: -------------------------------------------------------------------------------- 1 | export * from './getSnapshotWithoutPrototype' 2 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@antfu", 3 | "ignorePatterns": ["__fixtures__"] 4 | } 5 | -------------------------------------------------------------------------------- /packages/vitest-codemod/bin/vitest-codemod: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | require("../dist/cli.js"); 3 | -------------------------------------------------------------------------------- /packages/jest/jest.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "testMatch": ["**/src/__fixtures__/**/*.input.{js,mjs}"] 3 | } 4 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBe.input.js: -------------------------------------------------------------------------------- 1 | test("toBe", () => { 2 | expect("foo").toBe("foo"); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/not.input.js: -------------------------------------------------------------------------------- 1 | test("not", () => { 2 | expect("foo").not.toBe("bar"); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeNaN.input.js: -------------------------------------------------------------------------------- 1 | test("toBeNaN", () => { 2 | expect(NaN).toBeNaN(); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeNull.input.js: -------------------------------------------------------------------------------- 1 | test("toBeNull", () => { 2 | expect(null).toBeNull(); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-todo.input.js: -------------------------------------------------------------------------------- 1 | it.todo("test to be added"); 2 | test.todo("test to be added"); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/fit.input.js: -------------------------------------------------------------------------------- 1 | fit("Math.sqrt(4)", () => { 2 | expect(Math.sqrt(4)).toBe(2); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeDefined.input.js: -------------------------------------------------------------------------------- 1 | test("toBeDefined", () => { 2 | expect("foo").toBeDefined(); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeLessThan.input.js: -------------------------------------------------------------------------------- 1 | test("toBeLessThan", () => { 2 | expect(1).toBeLessThan(2); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toMatch.input.js: -------------------------------------------------------------------------------- 1 | test("toMatch", () => { 2 | expect("grapefruits").toMatch("fruit"); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/fit-fails.input.js: -------------------------------------------------------------------------------- 1 | fit.failing("Math.sqrt(4)", () => { 2 | expect(Math.sqrt(4)).toBe(3); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeGreaterThan.input.js: -------------------------------------------------------------------------------- 1 | test("toBeGreaterThan", () => { 2 | expect(2).toBeGreaterThan(1); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeUndefined.input.js: -------------------------------------------------------------------------------- 1 | test("toBeUndefined", () => { 2 | expect(undefined).toBeUndefined(); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toContain.input.js: -------------------------------------------------------------------------------- 1 | test("toContain", () => { 2 | expect(["foo", "bar"]).toContain("foo"); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/__mocks__/getToken.js: -------------------------------------------------------------------------------- 1 | const getToken = () => "tokenFromMocksFolder"; 2 | 3 | module.exports = {getToken}; -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/contexts.input.js: -------------------------------------------------------------------------------- 1 | test("todo", () => { 2 | // ToDo: https://github.com/vitest-dev/vitest/issues/2810 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/defaultExport.js: -------------------------------------------------------------------------------- 1 | // defaultExport.js is a module for testing default exports. 2 | module.exports = "default"; -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/getToken.js: -------------------------------------------------------------------------------- 1 | // Test function for mocking 2 | const getToken = () => "token"; 3 | 4 | module.exports = {getToken}; -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/not.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("not", () => { 3 | expect("foo").not.toBe("bar"); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBe.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toBe", () => { 3 | expect("foo").toBe("foo"); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeNaN.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toBeNaN", () => { 3 | expect(NaN).toBeNaN(); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/withImplementation.input.js: -------------------------------------------------------------------------------- 1 | test("todo", () => { 2 | // ToDo: https://github.com/vitest-dev/vitest/issues/2812 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-todo.output.js: -------------------------------------------------------------------------------- 1 | import { it, test } from "vitest"; 2 | it.todo("test to be added"); 3 | test.todo("test to be added"); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/hasAssertions.input.js: -------------------------------------------------------------------------------- 1 | test("hasAssertions", () => { 2 | expect.hasAssertions(); 3 | expect("foo").toBe("foo"); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeNull.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toBeNull", () => { 3 | expect(null).toBeNull(); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toContainEqual.input.js: -------------------------------------------------------------------------------- 1 | test("toContainEqual", () => { 2 | expect([{ foo: "bar" }]).toContainEqual({ foo: "bar" }); 3 | }); -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | /.yarn/** linguist-vendored 2 | /.yarn/releases/* binary 3 | /.yarn/plugins/**/* binary 4 | /.pnp.* binary linguist-generated -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/fit.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it } from "vitest"; 2 | it.only("Math.sqrt(4)", () => { 3 | expect(Math.sqrt(4)).toBe(2); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeDefined.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toBeDefined", () => { 3 | expect("foo").toBeDefined(); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeFalsy.input.js: -------------------------------------------------------------------------------- 1 | test.each([false, 0, '', null, undefined, NaN])("toBeFalsy", (value) => { 2 | expect(value).toBeFalsy(); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeLessThan.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toBeLessThan", () => { 3 | expect(1).toBeLessThan(2); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeTruthy.input.js: -------------------------------------------------------------------------------- 1 | test.each([false, 0, '', null, undefined, NaN])("toBeTruthy", (value) => { 2 | expect(value).not.toBeTruthy(); 3 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toHaveLength.input.js: -------------------------------------------------------------------------------- 1 | test("toHaveLength", () => { 2 | expect([1, 2, 3]).toHaveLength(3); 3 | expect("abc").toHaveLength(3); 4 | }); 5 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toMatch.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toMatch", () => { 3 | expect("grapefruits").toMatch("fruit"); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/getTokenModule.js: -------------------------------------------------------------------------------- 1 | // Test function for mocking 2 | const getTokenModule = () => "tokenFromModule"; 3 | 4 | module.exports = {getTokenModule}; -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/fit-fails.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it } from "vitest"; 2 | it.only.fails("Math.sqrt(4)", () => { 3 | expect(Math.sqrt(4)).toBe(3); 4 | }); -------------------------------------------------------------------------------- /packages/jest/tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.build.json", 3 | "compilerOptions": { 4 | "outDir": "dist", 5 | "rootDir": "src" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /packages/types/tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.build.json", 3 | "compilerOptions": { 4 | "outDir": "dist", 5 | "rootDir": "src" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "prettier.enable": false, 3 | "editor.formatOnSave": false, 4 | "editor.codeActionsOnSave": { 5 | "source.fixAll.eslint": true 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeGreaterThan.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toBeGreaterThan", () => { 3 | expect(2).toBeGreaterThan(1); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeUndefined.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toBeUndefined", () => { 3 | expect(undefined).toBeUndefined(); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toContain.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toContain", () => { 3 | expect(["foo", "bar"]).toContain("foo"); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/contexts.output.js: -------------------------------------------------------------------------------- 1 | import { test } from "vitest"; 2 | test("todo", () => { 3 | // ToDo: https://github.com/vitest-dev/vitest/issues/2810 4 | }); -------------------------------------------------------------------------------- /packages/vitest-codemod/src/utils/getTransforms.ts: -------------------------------------------------------------------------------- 1 | import { transform as jestTransform } from '@vitest-codemod/jest' 2 | 3 | export const getTransforms = () => [jestTransform] 4 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/isMockFunction.input.js: -------------------------------------------------------------------------------- 1 | test("isMockFunction", () => { 2 | const mock = jest.fn(); 3 | expect(jest.isMockFunction(mock)).toBe(true); 4 | }); 5 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/describe/describe-skip.input.js: -------------------------------------------------------------------------------- 1 | describe.skip("describe-skip", () => { 2 | test("Math.sqrt()", () => { 3 | expect(Math.sqrt(4)).toBe(3); 4 | }) 5 | }) 6 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeLessThanOrEqual.input.js: -------------------------------------------------------------------------------- 1 | test("toBeLessThanOrEqual", () => { 2 | expect(1).toBeLessThanOrEqual(1); 3 | expect(1).toBeLessThanOrEqual(2); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toMatchObject.input.js: -------------------------------------------------------------------------------- 1 | test("toMatchObject", () => { 2 | expect({ 3 | foo: "bar", 4 | baz: 42, 5 | }).toMatchObject({ foo: "bar" }); 6 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/withImplementation.output.js: -------------------------------------------------------------------------------- 1 | import { test } from "vitest"; 2 | test("todo", () => { 3 | // ToDo: https://github.com/vitest-dev/vitest/issues/2812 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/snapshots/getSnapshotWithoutPrototype.ts: -------------------------------------------------------------------------------- 1 | export const getSnapshotWithoutPrototype = (snapshot: string) => 2 | snapshot.replace('Array [', '[').replace('Object {', '{') 3 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/requireActual.input.js: -------------------------------------------------------------------------------- 1 | test("requireActual", () => { 2 | const { getToken } = jest.requireActual("./getToken"); 3 | expect(getToken()).toBe("token"); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/fit-each.input.js: -------------------------------------------------------------------------------- 1 | fit.each([ 2 | [4, 2], 3 | [9, 3], 4 | ])("Math.sqrt(%s) = %s", (input, output) => { 5 | expect(Math.sqrt(input)).toBe(output); 6 | }); -------------------------------------------------------------------------------- /.github/workflows/pull_request.yml: -------------------------------------------------------------------------------- 1 | name: pull_request 2 | 3 | on: 4 | pull_request: 5 | branches: [main] 6 | 7 | jobs: 8 | call-build: 9 | uses: ./.github/workflows/build.yml 10 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/arrayContaining.input.js: -------------------------------------------------------------------------------- 1 | test("arrayContaining", () => { 2 | expect(["foo", "bar", "baz"]).toEqual( 3 | expect.arrayContaining(["foo", "bar"]) 4 | ); 5 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeGreaterThanOrEqual.input.js: -------------------------------------------------------------------------------- 1 | test("toBeGreaterThanOrEqual", () => { 2 | expect(2).toBeGreaterThanOrEqual(1); 3 | expect(1).toBeGreaterThanOrEqual(1); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockName.input.js: -------------------------------------------------------------------------------- 1 | test("mockName", () => { 2 | const mockFn = jest.fn().mockName('mockedFunction'); 3 | mockFn(); 4 | expect(mockFn).toHaveBeenCalled(); 5 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/hasAssertions.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("hasAssertions", () => { 3 | expect.hasAssertions(); 4 | expect("foo").toBe("foo"); 5 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeInstanceOf.input.js: -------------------------------------------------------------------------------- 1 | test("toBeInstanceOf", () => { 2 | class A {} 3 | expect(new A()).toBeInstanceOf(A); 4 | expect(() => {}).toBeInstanceOf(Function); 5 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toContainEqual.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toContainEqual", () => { 3 | expect([{ foo: "bar" }]).toContainEqual({ foo: "bar" }); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/requireMock.input.js: -------------------------------------------------------------------------------- 1 | test("requireMock", () => { 2 | const { getToken } = jest.requireMock("./getToken"); 3 | expect(getToken()).toBe("tokenFromMocksFolder"); 4 | }); -------------------------------------------------------------------------------- /packages/vitest-codemod/src/utils/getUpdatedTransformFile.ts: -------------------------------------------------------------------------------- 1 | export const getUpdatedTransformFile = (transformName: string) => 2 | require.resolve(`@vitest-codemod/${transformName}/dist/transformer.js`) 3 | -------------------------------------------------------------------------------- /packages/vitest-codemod/src/utils/index.ts: -------------------------------------------------------------------------------- 1 | export * from './getHelpParagraph' 2 | export * from './getJsCodeshiftParser' 3 | export * from './getTransforms' 4 | export * from './getUpdatedTransformFile' 5 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/node14/tsconfig.json", 3 | "compilerOptions": { 4 | "declaration": true 5 | }, 6 | "exclude": ["dist", "**/__fixtures__/**", "**/*.spec.ts"] 7 | } 8 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/stringMatching.input.js: -------------------------------------------------------------------------------- 1 | test("stringMatching", () => { 2 | expect({ 3 | name: "foo bar baz" 4 | }).toEqual({ 5 | name: expect.stringMatching(/^foo/) 6 | }); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeFalsy.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test.each([false, 0, '', null, undefined, NaN])("toBeFalsy", (value) => { 3 | expect(value).toBeFalsy(); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-only.input.js: -------------------------------------------------------------------------------- 1 | it.only("Math.sqrt(4)", () => { 2 | expect(Math.sqrt(4)).toBe(2); 3 | }); 4 | 5 | test.only("Math.sqrt(4)", () => { 6 | expect(Math.sqrt(4)).toBe(2); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-skip.input.js: -------------------------------------------------------------------------------- 1 | it.skip("Math.sqrt(4)", () => { 2 | expect(Math.sqrt(4)).toBe(3); 3 | }); 4 | 5 | test.skip("Math.sqrt(4)", () => { 6 | expect(Math.sqrt(4)).toBe(3); 7 | }); -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.build.json", 3 | "compilerOptions": { 4 | "baseUrl": ".", 5 | "paths": { 6 | "@vitest-codemod/*": ["packages/*/src"] 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /.yarnrc.yml: -------------------------------------------------------------------------------- 1 | nodeLinker: node-modules 2 | 3 | plugins: 4 | - path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs 5 | spec: "@yarnpkg/plugin-workspace-tools" 6 | 7 | yarnPath: .yarn/releases/yarn-3.4.1.cjs 8 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toBeCalled.input.js: -------------------------------------------------------------------------------- 1 | test("toBeCalled", () => { 2 | const mockFn = jest.fn(); 3 | expect(mockFn).not.toBeCalled(); 4 | 5 | mockFn(); 6 | expect(mockFn).toBeCalled(); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/stringContaining.input.js: -------------------------------------------------------------------------------- 1 | test("stringContaining", () => { 2 | expect({ 3 | name: "foo bar baz" 4 | }).toEqual({ 5 | name: expect.stringContaining("foo") 6 | }); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeTruthy.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test.each([false, 0, '', null, undefined, NaN])("toBeTruthy", (value) => { 3 | expect(value).not.toBeTruthy(); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toHaveLength.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toHaveLength", () => { 3 | expect([1, 2, 3]).toHaveLength(3); 4 | expect("abc").toHaveLength(3); 5 | }); 6 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/unmock.input.js: -------------------------------------------------------------------------------- 1 | test("unmock", async () => { 2 | jest.unmock("./getToken"); 3 | 4 | const { getToken } = await import("./getToken"); 5 | expect(getToken()).toBe("token"); 6 | }); -------------------------------------------------------------------------------- /packages/vitest-codemod/tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../../tsconfig.build.json", 3 | "compilerOptions": { 4 | "outDir": "dist", 5 | "resolveJsonModule": true, 6 | "rootDir": "src" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/getMockName.input.js: -------------------------------------------------------------------------------- 1 | test("mockName", () => { 2 | const mockName = "mockName"; 3 | const mockFn = jest.fn().mockName(mockName); 4 | expect(mockFn.getMockName()).toBe(mockName); 5 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/isMockFunction.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("isMockFunction", () => { 3 | const mock = vi.fn(); 4 | expect(vi.isMockFunction(mock)).toBe(true); 5 | }); 6 | -------------------------------------------------------------------------------- /packages/jest/README.md: -------------------------------------------------------------------------------- 1 | # @vitest-codemod/jest 2 | 3 | > An internal package 4 | 5 | This package provides a transformer for vitest to jest. 6 | 7 | For usage, check [vitest-codemod](https://www.npmjs.com/package/vitest-codemod). -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeLessThanOrEqual.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toBeLessThanOrEqual", () => { 3 | expect(1).toBeLessThanOrEqual(1); 4 | expect(1).toBeLessThanOrEqual(2); 5 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toMatchObject.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toMatchObject", () => { 3 | expect({ 4 | foo: "bar", 5 | baz: 42, 6 | }).toMatchObject({ foo: "bar" }); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/deepUnmock.input.js: -------------------------------------------------------------------------------- 1 | test("deepUnmock", async () => { 2 | jest.deepUnmock("./getToken"); 3 | 4 | const { getToken } = await import("./getToken"); 5 | expect(getToken()).toBe("token"); 6 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-fails.input.js: -------------------------------------------------------------------------------- 1 | it.failing("Math.sqrt(4) = 3", () => { 2 | expect(Math.sqrt(4)).toBe(3); 3 | }); 4 | 5 | test.failing("Math.sqrt(4) = 3", () => { 6 | expect(Math.sqrt(4)).toBe(3); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/disableAutomock.input.js: -------------------------------------------------------------------------------- 1 | test("disableAutomock", async () => { 2 | jest.disableAutomock(); 3 | 4 | const { getToken } = await import("./getToken"); 5 | expect(getToken()).toBe("token"); 6 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/genMockFromModule.input.js: -------------------------------------------------------------------------------- 1 | test("genMockFromModule", () => { 2 | const { getTokenModule } = jest.genMockFromModule("./getTokenModule"); 3 | expect(getTokenModule._isMockFunction).toBe(true); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mock-named.input.js: -------------------------------------------------------------------------------- 1 | test("mock", async () => { 2 | jest.mock("./getToken"); 3 | 4 | const { getToken } = await import("./getToken"); 5 | expect(getToken()).toBe("tokenFromMocksFolder"); 6 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-concurrent.input.js: -------------------------------------------------------------------------------- 1 | it.concurrent("Math.sqrt()", () => { 2 | expect(Math.sqrt(4)).toBe(2); 3 | }); 4 | 5 | test.concurrent("Math.sqrt()", () => { 6 | expect(Math.sqrt(4)).toBe(2); 7 | }); 8 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-only-fails.input.js: -------------------------------------------------------------------------------- 1 | it.only.failing("Math.sqrt(4)", () => { 2 | expect(Math.sqrt(4)).toBe(3); 3 | }); 4 | 5 | test.only.failing("Math.sqrt(4)", () => { 6 | expect(Math.sqrt(4)).toBe(3); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-skip-fails.input.js: -------------------------------------------------------------------------------- 1 | it.skip.failing("Math.sqrt(4)", () => { 2 | expect(Math.sqrt(4)).toBe(3); 3 | }); 4 | 5 | test.skip.failing("Math.sqrt(4)", () => { 6 | expect(Math.sqrt(4)).toBe(3); 7 | }); -------------------------------------------------------------------------------- /packages/types/README.md: -------------------------------------------------------------------------------- 1 | # @vitest-codemod/types 2 | 3 | > An internal package 4 | 5 | This package provides types for vitest-codemod transforms. 6 | 7 | For usage, check [vitest-codemod](https://www.npmjs.com/package/vitest-codemod). -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toHaveBeenCalled.input.js: -------------------------------------------------------------------------------- 1 | test("toHaveBeenCalled", () => { 2 | const mockFn = jest.fn(); 3 | expect(mockFn).not.toHaveBeenCalled(); 4 | 5 | mockFn(); 6 | expect(mockFn).toHaveBeenCalled(); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/fit-each.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it } from "vitest"; 2 | it.only.each([ 3 | [4, 2], 4 | [9, 3], 5 | ])("Math.sqrt(%s) = %s", (input, output) => { 6 | expect(Math.sqrt(input)).toBe(output); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/describe/describe-skip.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe.skip("describe-skip", () => { 3 | test("Math.sqrt()", () => { 4 | expect(Math.sqrt(4)).toBe(3); 5 | }) 6 | }) 7 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/arrayContaining.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("arrayContaining", () => { 3 | expect(["foo", "bar", "baz"]).toEqual( 4 | expect.arrayContaining(["foo", "bar"]) 5 | ); 6 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeGreaterThanOrEqual.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toBeGreaterThanOrEqual", () => { 3 | expect(2).toBeGreaterThanOrEqual(1); 4 | expect(1).toBeGreaterThanOrEqual(1); 5 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/createMockFromModule.input.js: -------------------------------------------------------------------------------- 1 | test("createMockFromModule", () => { 2 | const { getTokenModule } = jest.createMockFromModule("./getTokenModule"); 3 | expect(getTokenModule._isMockFunction).toBe(true); 4 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/disableAutomock.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("disableAutomock", async () => { 3 | const { getToken } = await import("./getToken"); 4 | expect(getToken()).toBe("token"); 5 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockName.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockName", () => { 3 | const mockFn = vi.fn().mockName('mockedFunction'); 4 | mockFn(); 5 | expect(mockFn).toHaveBeenCalled(); 6 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeInstanceOf.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toBeInstanceOf", () => { 3 | class A {} 4 | expect(new A()).toBeInstanceOf(A); 5 | expect(() => {}).toBeInstanceOf(Function); 6 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/requireActual.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("requireActual", async () => { 3 | const { getToken } = await vi.importActual("./getToken"); 4 | expect(getToken()).toBe("token"); 5 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/toThrowErrorMatchingInlineSnapshot.input.js: -------------------------------------------------------------------------------- 1 | test("toThrowErrorMatchingInlineSnapshot", () => { 2 | expect(() => { 3 | throw new Error("error"); 4 | }).toThrowErrorMatchingInlineSnapshot(`"error"`); 5 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/describe/decribe-each.input.js: -------------------------------------------------------------------------------- 1 | describe.each([ 2 | [4, 2], 3 | [9, 3], 4 | ])("Math.sqrt(%s)", (input, output) => { 5 | test(`returns ${output}`, () => { 6 | expect(Math.sqrt(input)).toBe(output); 7 | }); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/misc/basic.input.js: -------------------------------------------------------------------------------- 1 | describe("basic", () => { 2 | test("Math.sqrt()", () => { 3 | expect(Math.sqrt(4)).toBe(2); 4 | expect(Math.sqrt(144)).toBe(12); 5 | expect(Math.sqrt(2)).toBe(Math.SQRT2); 6 | }) 7 | }) 8 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/stringMatching.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("stringMatching", () => { 3 | expect({ 4 | name: "foo bar baz" 5 | }).toEqual({ 6 | name: expect.stringMatching(/^foo/) 7 | }); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/fn.input.js: -------------------------------------------------------------------------------- 1 | test("fn", () => { 2 | const mockFn = jest.fn(); 3 | mockFn(); 4 | expect(mockFn).toHaveBeenCalled(); 5 | 6 | const mockReturnsTrue = jest.fn(() => true); 7 | expect(mockReturnsTrue()).toBe(true); 8 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/requireMock.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("requireMock", async () => { 3 | const { getToken } = await vi.importMock("./getToken"); 4 | expect(getToken()).toBe("tokenFromMocksFolder"); 5 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toBeCalled.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toBeCalled", () => { 3 | const mockFn = vi.fn(); 4 | expect(mockFn).not.toBeCalled(); 5 | 6 | mockFn(); 7 | expect(mockFn).toBeCalled(); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/describe/describe-skip-each.input.js: -------------------------------------------------------------------------------- 1 | describe.skip.each([ 2 | [4, 3], 3 | [9, 4], 4 | ])("Math.sqrt(%s)", (input, output) => { 5 | test(`returns ${output}`, () => { 6 | expect(Math.sqrt(input)).toBe(output); 7 | }); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/stringContaining.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("stringContaining", () => { 3 | expect({ 4 | name: "foo bar baz" 5 | }).toEqual({ 6 | name: expect.stringContaining("foo") 7 | }); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/unmock.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("unmock", async () => { 3 | vi.unmock("./getToken"); 4 | 5 | const { getToken } = await import("./getToken"); 6 | expect(getToken()).toBe("token"); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/__snapshots__/toThrowErrorMatchingSnapshot.output.js.snap: -------------------------------------------------------------------------------- 1 | // Vitest Snapshot v1 2 | 3 | exports[`toThrowErrorMatchingSnapshot > hint 1`] = `"error with hint"`; 4 | 5 | exports[`toThrowErrorMatchingSnapshot 1`] = `"error"`; 6 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-only.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.only("Math.sqrt(4)", () => { 3 | expect(Math.sqrt(4)).toBe(2); 4 | }); 5 | 6 | test.only("Math.sqrt(4)", () => { 7 | expect(Math.sqrt(4)).toBe(2); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-skip.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.skip("Math.sqrt(4)", () => { 3 | expect(Math.sqrt(4)).toBe(3); 4 | }); 5 | 6 | test.skip("Math.sqrt(4)", () => { 7 | expect(Math.sqrt(4)).toBe(3); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/getMockName.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockName", () => { 3 | const mockName = "mockName"; 4 | const mockFn = vi.fn().mockName(mockName); 5 | expect(mockFn.getMockName()).toBe(mockName); 6 | }) -------------------------------------------------------------------------------- /packages/jest/src/index.ts: -------------------------------------------------------------------------------- 1 | import type { VitestCodemodTransform } from '@vitest-codemod/types' 2 | 3 | export const transform: VitestCodemodTransform = { 4 | name: 'jest', 5 | description: 'Converts Jest APIs in a Javascript/TypeScript codebase to Vitest', 6 | } 7 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/deepUnmock.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("deepUnmock", async () => { 3 | vi.unmock("./getToken"); 4 | 5 | const { getToken } = await import("./getToken"); 6 | expect(getToken()).toBe("token"); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-fails.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.fails("Math.sqrt(4) = 3", () => { 3 | expect(Math.sqrt(4)).toBe(3); 4 | }); 5 | 6 | test.fails("Math.sqrt(4) = 3", () => { 7 | expect(Math.sqrt(4)).toBe(3); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mock-named.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mock", async () => { 3 | vi.mock("./getToken"); 4 | 5 | const { getToken } = await import("./getToken"); 6 | expect(getToken()).toBe("tokenFromMocksFolder"); 7 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/mockReturnValue.input.js: -------------------------------------------------------------------------------- 1 | test("mockReturnValue", () => { 2 | const mockFn = jest.fn(() => 1); 3 | expect(mockFn()).toBe(1); 4 | 5 | mockFn.mockReturnValue(42); 6 | expect(mockFn()).toBe(42); 7 | expect(mockFn()).toBe(42); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-only-fails.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.only.fails("Math.sqrt(4)", () => { 3 | expect(Math.sqrt(4)).toBe(3); 4 | }); 5 | 6 | test.only.fails("Math.sqrt(4)", () => { 7 | expect(Math.sqrt(4)).toBe(3); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-skip-fails.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.skip.fails("Math.sqrt(4)", () => { 3 | expect(Math.sqrt(4)).toBe(3); 4 | }); 5 | 6 | test.skip.fails("Math.sqrt(4)", () => { 7 | expect(Math.sqrt(4)).toBe(3); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/genMockFromModule.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("genMockFromModule", async () => { 3 | const { getTokenModule } = await vi.importMock("./getTokenModule"); 4 | expect(getTokenModule._isMockFunction).toBe(true); 5 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/setMock-default.input.js: -------------------------------------------------------------------------------- 1 | test("setMock", async () => { 2 | jest.setMock("./defaultExport", "defaultFromMock"); 3 | 4 | const { default: defaultExport } = await import("./defaultExport"); 5 | expect(defaultExport).toBe("defaultFromMock"); 6 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/__snapshots__/toThrowErrorMatchingSnapshot.input.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`toThrowErrorMatchingSnapshot 1`] = `"error"`; 4 | 5 | exports[`toThrowErrorMatchingSnapshot: hint 1`] = `"error with hint"`; 6 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-concurrent.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.concurrent("Math.sqrt()", () => { 3 | expect(Math.sqrt(4)).toBe(2); 4 | }); 5 | 6 | test.concurrent("Math.sqrt()", () => { 7 | expect(Math.sqrt(4)).toBe(2); 8 | }); 9 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toHaveBeenCalled.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toHaveBeenCalled", () => { 3 | const mockFn = vi.fn(); 4 | expect(mockFn).not.toHaveBeenCalled(); 5 | 6 | mockFn(); 7 | expect(mockFn).toHaveBeenCalled(); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/describe/describe-only.input.js: -------------------------------------------------------------------------------- 1 | describe.only("describe-only", () => { 2 | test("Math.sqrt()", () => { 3 | expect(Math.sqrt(4)).toBe(2); 4 | }) 5 | }); 6 | 7 | test('will not be run', () => { 8 | expect(Math.sqrt(4)).toBe(3); 9 | }); 10 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockImplementation.input.js: -------------------------------------------------------------------------------- 1 | test("mockImplementation", () => { 2 | const mockFn = jest.fn(() => 1); 3 | expect(mockFn()).toBe(1); 4 | 5 | mockFn.mockImplementation(() => 42); 6 | expect(mockFn()).toBe(42); 7 | expect(mockFn()).toBe(42); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/__snapshots__/addSnapshotSerializer.output.js.snap: -------------------------------------------------------------------------------- 1 | // Vitest Snapshot v1 2 | 3 | exports[`addSnapshotSerializer > toMatchSnapshot > object 1`] = `{"foo":"bar"}`; 4 | 5 | exports[`addSnapshotSerializer > toMatchSnapshot > string 1`] = `"FOOBAR"`; 6 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toBeCalledTimes.input.js: -------------------------------------------------------------------------------- 1 | test("toBeCalledTimes", () => { 2 | const mockFn = jest.fn(); 3 | expect(mockFn).toBeCalledTimes(0); 4 | 5 | mockFn(); 6 | expect(mockFn).toBeCalledTimes(1); 7 | mockFn(); 8 | expect(mockFn).toBeCalledTimes(2); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/createMockFromModule.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("createMockFromModule", async () => { 3 | const { getTokenModule } = await vi.importMock("./getTokenModule"); 4 | expect(getTokenModule._isMockFunction).toBe(true); 5 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/setMock-named.input.js: -------------------------------------------------------------------------------- 1 | test("setMock", async () => { 2 | jest.setMock("./getToken", { 3 | getToken: () => "tokenFromFactory", 4 | }); 5 | 6 | const { getToken } = await import("./getToken"); 7 | expect(getToken()).toBe("tokenFromFactory"); 8 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/mockReturnValueOnce.input.js: -------------------------------------------------------------------------------- 1 | test("mockReturnValueOnce", () => { 2 | const mockFn = jest.fn(() => 1); 3 | expect(mockFn()).toBe(1); 4 | 5 | mockFn.mockReturnValueOnce(42); 6 | expect(mockFn()).toBe(42); 7 | 8 | expect(mockFn()).toBe(1); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toReturnWith.input.js: -------------------------------------------------------------------------------- 1 | test("toReturnWith", () => { 2 | const mockFn = jest.fn(input => `mockFn(${input})`); 3 | mockFn("foo"); 4 | mockFn("bar"); 5 | expect(mockFn).toReturnWith(`mockFn(foo)`); 6 | expect(mockFn).toReturnWith(`mockFn(bar)`); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/toThrowErrorMatchingInlineSnapshot.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toThrowErrorMatchingInlineSnapshot", () => { 3 | expect(() => { 4 | throw new Error("error"); 5 | }).toThrowErrorMatchingInlineSnapshot(`"error"`); 6 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mock-default-arrow-fn.input.js: -------------------------------------------------------------------------------- 1 | test("mockDefault", async () => { 2 | jest.mock("./defaultExport", () => "defaultFromMock"); 3 | 4 | const { default: defaultExport } = await import("./defaultExport"); 5 | expect(defaultExport).toBe("defaultFromMock"); 6 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/__snapshots__/addSnapshotSerializer.input.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`addSnapshotSerializer toMatchSnapshot object 1`] = `{"foo":"bar"}`; 4 | 5 | exports[`addSnapshotSerializer toMatchSnapshot string 1`] = `"FOOBAR"`; 6 | -------------------------------------------------------------------------------- /vitest.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vitest/config' 2 | 3 | export default defineConfig({ 4 | test: { 5 | allowOnly: true, 6 | forceRerunTriggers: ['**/__fixtures__/**'], 7 | include: ['**/src/__fixtures__/**/*.output.{mjs,js,ts}', '**/*.spec.ts'], 8 | }, 9 | }) 10 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/describe/decribe-each.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe.each([ 3 | [4, 2], 4 | [9, 3], 5 | ])("Math.sqrt(%s)", (input, output) => { 6 | test(`returns ${output}`, () => { 7 | expect(Math.sqrt(input)).toBe(output); 8 | }); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/fn.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("fn", () => { 3 | const mockFn = vi.fn(); 4 | mockFn(); 5 | expect(mockFn).toHaveBeenCalled(); 6 | 7 | const mockReturnsTrue = vi.fn(() => true); 8 | expect(mockReturnsTrue()).toBe(true); 9 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockImplementationOnce.input.js: -------------------------------------------------------------------------------- 1 | test("mockImplementationOnce", () => { 2 | const mockFn = jest.fn(() => 1); 3 | expect(mockFn()).toBe(1); 4 | 5 | mockFn.mockImplementationOnce(() => 42); 6 | expect(mockFn()).toBe(42); 7 | 8 | expect(mockFn()).toBe(1); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/misc/basic.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe("basic", () => { 3 | test("Math.sqrt()", () => { 4 | expect(Math.sqrt(4)).toBe(2); 5 | expect(Math.sqrt(144)).toBe(12); 6 | expect(Math.sqrt(2)).toBe(Math.SQRT2); 7 | }) 8 | }) 9 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/misc/with-top-line-comment.input.js: -------------------------------------------------------------------------------- 1 | // This test is for math operations 2 | describe("basic", () => { 3 | test("Math.sqrt()", () => { 4 | expect(Math.sqrt(4)).toBe(2); 5 | expect(Math.sqrt(144)).toBe(12); 6 | expect(Math.sqrt(2)).toBe(Math.SQRT2); 7 | }) 8 | }) 9 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mock-factory-named.input.js: -------------------------------------------------------------------------------- 1 | test("mock", async () => { 2 | jest.mock("./getToken", () => ({ 3 | getToken: jest.fn(() => "tokenFromFactory"), 4 | })); 5 | 6 | const { getToken } = await import("./getToken"); 7 | expect(getToken()).toBe("tokenFromFactory"); 8 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/lastReturnedWith.input.js: -------------------------------------------------------------------------------- 1 | test("lastReturnedWith", () => { 2 | const mockFn = jest.fn(input => `mockFn(${input})`); 3 | mockFn("foo"); 4 | expect(mockFn).lastReturnedWith(`mockFn(foo)`); 5 | mockFn("bar"); 6 | expect(mockFn).lastReturnedWith(`mockFn(bar)`); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/nthReturnedWith.input.js: -------------------------------------------------------------------------------- 1 | test("nthReturnedWith", () => { 2 | const mockFn = jest.fn(input => `mockFn(${input})`); 3 | mockFn("foo"); 4 | mockFn("bar"); 5 | expect(mockFn).nthReturnedWith(1, `mockFn(foo)`); 6 | expect(mockFn).nthReturnedWith(2, `mockFn(bar)`); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/describe/describe-skip-each.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe.skip.each([ 3 | [4, 3], 4 | [9, 4], 5 | ])("Math.sqrt(%s)", (input, output) => { 6 | test(`returns ${output}`, () => { 7 | expect(Math.sqrt(input)).toBe(output); 8 | }); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toHaveReturnedWith.input.js: -------------------------------------------------------------------------------- 1 | test("toHaveReturnedWith", () => { 2 | const mockFn = jest.fn(input => `mockFn(${input})`); 3 | mockFn("foo"); 4 | mockFn("bar"); 5 | expect(mockFn).toHaveReturnedWith(`mockFn(foo)`); 6 | expect(mockFn).toHaveReturnedWith(`mockFn(bar)`); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toHaveBeenCalledTimes.input.js: -------------------------------------------------------------------------------- 1 | test("toHaveBeenCalledTimes", () => { 2 | const mockFn = jest.fn(); 3 | expect(mockFn).toHaveBeenCalledTimes(0); 4 | 5 | mockFn(); 6 | expect(mockFn).toHaveBeenCalledTimes(1); 7 | mockFn(); 8 | expect(mockFn).toHaveBeenCalledTimes(2); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/mockReturnValue.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockReturnValue", () => { 3 | const mockFn = vi.fn(() => 1); 4 | expect(mockFn()).toBe(1); 5 | 6 | mockFn.mockReturnValue(42); 7 | expect(mockFn()).toBe(42); 8 | expect(mockFn()).toBe(42); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mock-default-fn-expr.input.js: -------------------------------------------------------------------------------- 1 | test("mockDefault", async () => { 2 | jest.mock("./defaultExport", function() { 3 | return "defaultFromMock"; 4 | }); 5 | 6 | const { default: defaultExport } = await import("./defaultExport"); 7 | expect(defaultExport).toBe("defaultFromMock"); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/mockResolvedValue.input.js: -------------------------------------------------------------------------------- 1 | test("mockResolvedValue", () => { 2 | const mockFn = jest.fn(() => Promise.resolve(1)); 3 | expect(mockFn()).resolves.toBe(1); 4 | 5 | mockFn.mockResolvedValue(42); 6 | expect(mockFn()).resolves.toBe(42); 7 | expect(mockFn()).resolves.toBe(42); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockImplementation.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockImplementation", () => { 3 | const mockFn = vi.fn(() => 1); 4 | expect(mockFn()).toBe(1); 5 | 6 | mockFn.mockImplementation(() => 42); 7 | expect(mockFn()).toBe(42); 8 | expect(mockFn()).toBe(42); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toHaveLastReturnedWith.input.js: -------------------------------------------------------------------------------- 1 | test("toHaveLastReturnedWith", () => { 2 | const mockFn = jest.fn(input => `mockFn(${input})`); 3 | mockFn("foo"); 4 | expect(mockFn).toHaveLastReturnedWith(`mockFn(foo)`); 5 | mockFn("bar"); 6 | expect(mockFn).toHaveLastReturnedWith(`mockFn(bar)`); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/__snapshots__/toMatchSnapshot.output.js.snap: -------------------------------------------------------------------------------- 1 | // Vitest Snapshot v1 2 | 3 | exports[`toMatchSnapshot > object 1`] = ` 4 | { 5 | "createdAt": Any, 6 | "id": Any, 7 | "name": "John Doe", 8 | } 9 | `; 10 | 11 | exports[`toMatchSnapshot > string 1`] = `"FOOBAR"`; 12 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### Issue 2 | 3 | Issue number, if available, prefixed with "#" 4 | 5 | ### Description 6 | 7 | What does this implement/fix? Explain your changes. 8 | 9 | ### Testing 10 | 11 | How was this change tested? 12 | 13 | ### Additional context 14 | 15 | Add any other context about the PR here. 16 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/objectContaining.input.js: -------------------------------------------------------------------------------- 1 | test("objectContaining", () => { 2 | expect({ 3 | examples: [ 4 | { 5 | foo: "bar", 6 | baz: 42, 7 | } 8 | ] 9 | }).toEqual({ 10 | examples: [ 11 | expect.objectContaining({foo: "bar" }) 12 | ] 13 | }); 14 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/misc/replace-jest-imports.output.mjs: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | 3 | describe("basic", () => { 4 | test("Math.sqrt()", () => { 5 | expect(Math.sqrt(4)).toBe(2); 6 | expect(Math.sqrt(144)).toBe(12); 7 | expect(Math.sqrt(2)).toBe(Math.SQRT2); 8 | }) 9 | }) 10 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/mockResolvedValueOnce.input.js: -------------------------------------------------------------------------------- 1 | test("mockResolvedValue", () => { 2 | const mockFn = jest.fn(() => Promise.resolve(1)); 3 | expect(mockFn()).resolves.toBe(1); 4 | 5 | mockFn.mockResolvedValueOnce(42); 6 | expect(mockFn()).resolves.toBe(42); 7 | 8 | expect(mockFn()).resolves.toBe(1); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toHaveNthReturnedWith.input.js: -------------------------------------------------------------------------------- 1 | test("toHaveNthReturnedWith", () => { 2 | const mockFn = jest.fn(input => `mockFn(${input})`); 3 | mockFn("foo"); 4 | mockFn("bar"); 5 | expect(mockFn).toHaveNthReturnedWith(1, `mockFn(foo)`); 6 | expect(mockFn).toHaveNthReturnedWith(2, `mockFn(bar)`); 7 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toReturnWith.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toReturnWith", () => { 3 | const mockFn = vi.fn(input => `mockFn(${input})`); 4 | mockFn("foo"); 5 | mockFn("bar"); 6 | expect(mockFn).toReturnWith(`mockFn(foo)`); 7 | expect(mockFn).toReturnWith(`mockFn(bar)`); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/toThrowErrorMatchingSnapshot.input.js: -------------------------------------------------------------------------------- 1 | test("toThrowErrorMatchingSnapshot", () => { 2 | expect(() => { 3 | throw new Error("error"); 4 | }).toThrowErrorMatchingSnapshot(); 5 | 6 | expect(() => { 7 | throw new Error("error with hint"); 8 | }).toThrowErrorMatchingSnapshot("hint"); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/describe/describe-only.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe.only("describe-only", () => { 3 | test("Math.sqrt()", () => { 4 | expect(Math.sqrt(4)).toBe(2); 5 | }) 6 | }); 7 | 8 | test('will not be run', () => { 9 | expect(Math.sqrt(4)).toBe(3); 10 | }); 11 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/misc/replace-jest-imports.input.mjs: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "@jest/globals"; 2 | 3 | describe("basic", () => { 4 | test("Math.sqrt()", () => { 5 | expect(Math.sqrt(4)).toBe(2); 6 | expect(Math.sqrt(144)).toBe(12); 7 | expect(Math.sqrt(2)).toBe(Math.SQRT2); 8 | }) 9 | }) 10 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/setMock-named.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("setMock", async () => { 3 | vi.mock("./getToken", () => ({ 4 | getToken: () => "tokenFromFactory" 5 | })); 6 | 7 | const { getToken } = await import("./getToken"); 8 | expect(getToken()).toBe("tokenFromFactory"); 9 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toBeCalledTimes.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toBeCalledTimes", () => { 3 | const mockFn = vi.fn(); 4 | expect(mockFn).toBeCalledTimes(0); 5 | 6 | mockFn(); 7 | expect(mockFn).toBeCalledTimes(1); 8 | mockFn(); 9 | expect(mockFn).toBeCalledTimes(2); 10 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/misc/with-file-headers.input.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Example comment which should not be replaced. 3 | */ 4 | 5 | describe("basic", () => { 6 | test("Math.sqrt()", () => { 7 | expect(Math.sqrt(4)).toBe(2); 8 | expect(Math.sqrt(144)).toBe(12); 9 | expect(Math.sqrt(2)).toBe(Math.SQRT2); 10 | }) 11 | }) 12 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/mockReturnValueOnce.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockReturnValueOnce", () => { 3 | const mockFn = vi.fn(() => 1); 4 | expect(mockFn()).toBe(1); 5 | 6 | mockFn.mockReturnValueOnce(42); 7 | expect(mockFn()).toBe(42); 8 | 9 | expect(mockFn()).toBe(1); 10 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/__snapshots__/toMatchSnapshot.input.js.snap: -------------------------------------------------------------------------------- 1 | // Jest Snapshot v1, https://goo.gl/fbAQLP 2 | 3 | exports[`toMatchSnapshot object 1`] = ` 4 | { 5 | "createdAt": Any, 6 | "id": Any, 7 | "name": "John Doe", 8 | } 9 | `; 10 | 11 | exports[`toMatchSnapshot string 1`] = `"FOOBAR"`; 12 | -------------------------------------------------------------------------------- /packages/jest/src/apis/index.ts: -------------------------------------------------------------------------------- 1 | export * from './addFactoryFunctionToMock' 2 | export * from './getApisFromCallExpression' 3 | export * from './getApisFromMemberExpression' 4 | export * from './replaceJestObjectWithVi' 5 | export * from './replaceTestApiFailing' 6 | export * from './replaceTestApiFit' 7 | export * from './updateDefaultExportMocks' 8 | -------------------------------------------------------------------------------- /.changeset/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://unpkg.com/@changesets/config@2.3.0/schema.json", 3 | "changelog": [ 4 | "@changesets/changelog-github", 5 | { "repo": "trivikr/vitest-codemod" } 6 | ], 7 | "commit": false, 8 | "access": "public", 9 | "baseBranch": "main", 10 | "updateInternalDependencies": "patch" 11 | } 12 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/lastCall.input.js: -------------------------------------------------------------------------------- 1 | test("lastCall", () => { 2 | const mockFn = jest.fn(); 3 | expect(mockFn.mock.lastCall).toBeUndefined(); 4 | 5 | mockFn("foo"); 6 | expect(mockFn.mock.lastCall).toEqual(["foo"]); 7 | 8 | mockFn("bar", "baz"); 9 | expect(mockFn.mock.lastCall).toEqual(["bar", "baz"]); 10 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/lastCalledWith.input.js: -------------------------------------------------------------------------------- 1 | test("lastCalledWith", () => { 2 | const mockFn = jest.fn(); 3 | 4 | mockFn(); 5 | expect(mockFn).lastCalledWith(); 6 | 7 | mockFn("foo"); 8 | expect(mockFn).lastCalledWith("foo"); 9 | 10 | mockFn("foo", "bar"); 11 | expect(mockFn).lastCalledWith("foo", "bar"); 12 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toBeCalledWith.input.js: -------------------------------------------------------------------------------- 1 | test("toBeCalledWith", () => { 2 | const mockFn = jest.fn(); 3 | 4 | mockFn(); 5 | mockFn("foo"); 6 | mockFn("foo", "bar"); 7 | 8 | expect(mockFn).toBeCalledWith(); 9 | expect(mockFn).toBeCalledWith("foo"); 10 | expect(mockFn).toBeCalledWith("foo", "bar"); 11 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mock-factory-named.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mock", async () => { 3 | vi.mock("./getToken", () => ({ 4 | getToken: vi.fn(() => "tokenFromFactory"), 5 | })); 6 | 7 | const { getToken } = await import("./getToken"); 8 | expect(getToken()).toBe("tokenFromFactory"); 9 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/nthCalledWith.input.js: -------------------------------------------------------------------------------- 1 | test("nthCalledWith", () => { 2 | const mockFn = jest.fn(); 3 | 4 | mockFn(); 5 | expect(mockFn).nthCalledWith(1); 6 | 7 | mockFn("foo"); 8 | expect(mockFn).nthCalledWith(2, "foo"); 9 | 10 | mockFn("foo", "bar"); 11 | expect(mockFn).nthCalledWith(3, "foo", "bar"); 12 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/describe/describe-only-each.input.js: -------------------------------------------------------------------------------- 1 | describe.only.each([ 2 | [4, 2], 3 | [9, 3], 4 | ])("Math.sqrt(%s)", (input, output) => { 5 | test(`returns ${output}`, () => { 6 | expect(Math.sqrt(input)).toBe(output); 7 | }); 8 | }); 9 | 10 | test('will not be run', () => { 11 | expect(Math.sqrt(4)).toBe(3); 12 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/lastReturnedWith.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("lastReturnedWith", () => { 3 | const mockFn = vi.fn(input => `mockFn(${input})`); 4 | mockFn("foo"); 5 | expect(mockFn).lastReturnedWith(`mockFn(foo)`); 6 | mockFn("bar"); 7 | expect(mockFn).lastReturnedWith(`mockFn(bar)`); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/nthReturnedWith.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("nthReturnedWith", () => { 3 | const mockFn = vi.fn(input => `mockFn(${input})`); 4 | mockFn("foo"); 5 | mockFn("bar"); 6 | expect(mockFn).nthReturnedWith(1, `mockFn(foo)`); 7 | expect(mockFn).nthReturnedWith(2, `mockFn(bar)`); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/toMatchInlineSnapshot.input.js: -------------------------------------------------------------------------------- 1 | describe("toMatchInlineSnapshot", () => { 2 | test("string", () => { 3 | expect("foobar".toUpperCase()).toMatchInlineSnapshot(`"FOOBAR"`); 4 | }); 5 | 6 | test("object", () => { 7 | expect({ foo: "bar" }).toMatchInlineSnapshot(`\n{\n "foo": "bar",\n}\n`); 8 | }); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-each.input.js: -------------------------------------------------------------------------------- 1 | it.each([ 2 | [4, 2], 3 | [9, 3], 4 | ])("Math.sqrt(%s) = %s", (input, output) => { 5 | expect(Math.sqrt(input)).toBe(output); 6 | }); 7 | 8 | test.each([ 9 | [4, 2], 10 | [9, 3], 11 | ])("Math.sqrt(%s) = %s", (input, output) => { 12 | expect(Math.sqrt(input)).toBe(output); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toStrictEqual.input.js: -------------------------------------------------------------------------------- 1 | class Stock { 2 | constructor(type) { 3 | this.type = type; 4 | } 5 | } 6 | 7 | test("structurally the same, but semantically different", () => { 8 | expect(new Stock("apples")).toEqual({ type: "apples" }); 9 | expect(new Stock("apples")).not.toStrictEqual({ type: "apples" }); 10 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockImplementationOnce.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockImplementationOnce", () => { 3 | const mockFn = vi.fn(() => 1); 4 | expect(mockFn()).toBe(1); 5 | 6 | mockFn.mockImplementationOnce(() => 42); 7 | expect(mockFn()).toBe(42); 8 | 9 | expect(mockFn()).toBe(1); 10 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/setMock-default.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("setMock", async () => { 3 | vi.mock("./defaultExport", () => ({ 4 | default: "defaultFromMock" 5 | })); 6 | 7 | const { default: defaultExport } = await import("./defaultExport"); 8 | expect(defaultExport).toBe("defaultFromMock"); 9 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toHaveReturnedWith.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toHaveReturnedWith", () => { 3 | const mockFn = vi.fn(input => `mockFn(${input})`); 4 | mockFn("foo"); 5 | mockFn("bar"); 6 | expect(mockFn).toHaveReturnedWith(`mockFn(foo)`); 7 | expect(mockFn).toHaveReturnedWith(`mockFn(bar)`); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/misc/with-top-line-comment.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | // This test is for math operations 3 | describe("basic", () => { 4 | test("Math.sqrt()", () => { 5 | expect(Math.sqrt(4)).toBe(2); 6 | expect(Math.sqrt(144)).toBe(12); 7 | expect(Math.sqrt(2)).toBe(Math.SQRT2); 8 | }) 9 | }) 10 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mock-default-arrow-fn.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockDefault", async () => { 3 | vi.mock("./defaultExport", () => ({ 4 | default: "defaultFromMock" 5 | })); 6 | 7 | const { default: defaultExport } = await import("./defaultExport"); 8 | expect(defaultExport).toBe("defaultFromMock"); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/mockResolvedValue.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockResolvedValue", () => { 3 | const mockFn = vi.fn(() => Promise.resolve(1)); 4 | expect(mockFn()).resolves.toBe(1); 5 | 6 | mockFn.mockResolvedValue(42); 7 | expect(mockFn()).resolves.toBe(42); 8 | expect(mockFn()).resolves.toBe(42); 9 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-only-each.input.js: -------------------------------------------------------------------------------- 1 | it.only.each([ 2 | [4, 2], 3 | [9, 3], 4 | ])("Math.sqrt(%s) = %s", (input, output) => { 5 | expect(Math.sqrt(input)).toBe(output); 6 | }); 7 | 8 | test.only.each([ 9 | [4, 2], 10 | [9, 3], 11 | ])("Math.sqrt(%s) = %s", (input, output) => { 12 | expect(Math.sqrt(input)).toBe(output); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-skip-each.input.js: -------------------------------------------------------------------------------- 1 | it.skip.each([ 2 | [4, 3], 3 | [9, 4], 4 | ])("Math.sqrt(%s) = %s", (input, output) => { 5 | expect(Math.sqrt(input)).toBe(output); 6 | }); 7 | 8 | test.skip.each([ 9 | [4, 3], 10 | [9, 4], 11 | ])("Math.sqrt(%s) = %s", (input, output) => { 12 | expect(Math.sqrt(input)).toBe(output); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toHaveBeenCalledTimes.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toHaveBeenCalledTimes", () => { 3 | const mockFn = vi.fn(); 4 | expect(mockFn).toHaveBeenCalledTimes(0); 5 | 6 | mockFn(); 7 | expect(mockFn).toHaveBeenCalledTimes(1); 8 | mockFn(); 9 | expect(mockFn).toHaveBeenCalledTimes(2); 10 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toHaveLastReturnedWith.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toHaveLastReturnedWith", () => { 3 | const mockFn = vi.fn(input => `mockFn(${input})`); 4 | mockFn("foo"); 5 | expect(mockFn).toHaveLastReturnedWith(`mockFn(foo)`); 6 | mockFn("bar"); 7 | expect(mockFn).toHaveLastReturnedWith(`mockFn(bar)`); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-fails-each.input.js: -------------------------------------------------------------------------------- 1 | it.failing.each([ 2 | [4, 3], 3 | [9, 4], 4 | ])("Math.sqrt(%s) = %s", (input, output) => { 5 | expect(Math.sqrt(input)).toBe(output); 6 | }); 7 | 8 | test.failing.each([ 9 | [4, 3], 10 | [9, 4], 11 | ])("Math.sqrt(%s) = %s", (input, output) => { 12 | expect(Math.sqrt(input)).toBe(output); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/async/resolves.input.js: -------------------------------------------------------------------------------- 1 | describe("resolves", () => { 2 | const mockData = "data"; 3 | const dataFn = async () => mockData; 4 | 5 | test("return", () => { 6 | return expect(dataFn()).resolves.toBe(mockData); 7 | }); 8 | 9 | test("await", async () => { 10 | await expect(dataFn()).resolves.toBe(mockData); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toHaveBeenCalledWith.input.js: -------------------------------------------------------------------------------- 1 | test("toHaveBeenCalledWith", () => { 2 | const mockFn = jest.fn(); 3 | 4 | mockFn(); 5 | mockFn("foo"); 6 | mockFn("foo", "bar"); 7 | 8 | expect(mockFn).toHaveBeenCalledWith(); 9 | expect(mockFn).toHaveBeenCalledWith("foo"); 10 | expect(mockFn).toHaveBeenCalledWith("foo", "bar"); 11 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/objectContaining.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("objectContaining", () => { 3 | expect({ 4 | examples: [ 5 | { 6 | foo: "bar", 7 | baz: 42, 8 | } 9 | ] 10 | }).toEqual({ 11 | examples: [ 12 | expect.objectContaining({foo: "bar" }) 13 | ] 14 | }); 15 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toHaveNthReturnedWith.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toHaveNthReturnedWith", () => { 3 | const mockFn = vi.fn(input => `mockFn(${input})`); 4 | mockFn("foo"); 5 | mockFn("bar"); 6 | expect(mockFn).toHaveNthReturnedWith(1, `mockFn(foo)`); 7 | expect(mockFn).toHaveNthReturnedWith(2, `mockFn(bar)`); 8 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/misc/with-existing-imports.input.mjs: -------------------------------------------------------------------------------- 1 | /* 2 | * Example comment which should not be replaced. 3 | */ 4 | import assert from 'node:assert'; 5 | 6 | describe("basic", () => { 7 | test("Math.sqrt()", () => { 8 | assert(Math.sqrt(4) === 2); 9 | assert(Math.sqrt(144) === 12); 10 | assert(Math.sqrt(2) === Math.SQRT2); 11 | }) 12 | }) 13 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toReturn.input.js: -------------------------------------------------------------------------------- 1 | test("toReturn", () => { 2 | const mockError = new Error("mockError"); 3 | const mockFn = jest.fn(() => { throw mockError; }); 4 | 5 | try { 6 | mockFn(); 7 | } catch (error) {} 8 | expect(mockFn).not.toReturn(); 9 | 10 | mockFn.mockReturnValue(42); 11 | mockFn(); 12 | expect(mockFn).toReturn(); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-concurrent-each.input.js: -------------------------------------------------------------------------------- 1 | it.concurrent.each([ 2 | [4, 2], 3 | [9, 3], 4 | ])("Math.sqrt(%s) = %s", (input, output) => { 5 | expect(Math.sqrt(input)).toBe(output); 6 | }); 7 | 8 | test.concurrent.each([ 9 | [4, 2], 10 | [9, 3], 11 | ])("Math.sqrt(%s) = %s", (input, output) => { 12 | expect(Math.sqrt(input)).toBe(output); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/mockResolvedValueOnce.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockResolvedValue", () => { 3 | const mockFn = vi.fn(() => Promise.resolve(1)); 4 | expect(mockFn()).resolves.toBe(1); 5 | 6 | mockFn.mockResolvedValueOnce(42); 7 | expect(mockFn()).resolves.toBe(42); 8 | 9 | expect(mockFn()).resolves.toBe(1); 10 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/toThrowErrorMatchingSnapshot.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toThrowErrorMatchingSnapshot", () => { 3 | expect(() => { 4 | throw new Error("error"); 5 | }).toThrowErrorMatchingSnapshot(); 6 | 7 | expect(() => { 8 | throw new Error("error with hint"); 9 | }).toThrowErrorMatchingSnapshot("hint"); 10 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/lastCall.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("lastCall", () => { 3 | const mockFn = vi.fn(); 4 | expect(mockFn.mock.lastCall).toBeUndefined(); 5 | 6 | mockFn("foo"); 7 | expect(mockFn.mock.lastCall).toEqual(["foo"]); 8 | 9 | mockFn("bar", "baz"); 10 | expect(mockFn.mock.lastCall).toEqual(["bar", "baz"]); 11 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toBeCalledWith.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toBeCalledWith", () => { 3 | const mockFn = vi.fn(); 4 | 5 | mockFn(); 6 | mockFn("foo"); 7 | mockFn("foo", "bar"); 8 | 9 | expect(mockFn).toBeCalledWith(); 10 | expect(mockFn).toBeCalledWith("foo"); 11 | expect(mockFn).toBeCalledWith("foo", "bar"); 12 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/lastCalledWith.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("lastCalledWith", () => { 3 | const mockFn = vi.fn(); 4 | 5 | mockFn(); 6 | expect(mockFn).lastCalledWith(); 7 | 8 | mockFn("foo"); 9 | expect(mockFn).lastCalledWith("foo"); 10 | 11 | mockFn("foo", "bar"); 12 | expect(mockFn).lastCalledWith("foo", "bar"); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/nthCalledWith.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("nthCalledWith", () => { 3 | const mockFn = vi.fn(); 4 | 5 | mockFn(); 6 | expect(mockFn).nthCalledWith(1); 7 | 8 | mockFn("foo"); 9 | expect(mockFn).nthCalledWith(2, "foo"); 10 | 11 | mockFn("foo", "bar"); 12 | expect(mockFn).nthCalledWith(3, "foo", "bar"); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toHaveBeenLastCalledWith.input.js: -------------------------------------------------------------------------------- 1 | test("toHaveBeenLastCalledWith", () => { 2 | const mockFn = jest.fn(); 3 | 4 | mockFn(); 5 | expect(mockFn).toHaveBeenLastCalledWith(); 6 | 7 | mockFn("foo"); 8 | expect(mockFn).toHaveBeenLastCalledWith("foo"); 9 | 10 | mockFn("foo", "bar"); 11 | expect(mockFn).toHaveBeenLastCalledWith("foo", "bar"); 12 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-concurrent-only-each.input.js: -------------------------------------------------------------------------------- 1 | it.concurrent.only.each([ 2 | [4, 2], 3 | [9, 3], 4 | ])("Math.sqrt(%s) = %s", (input, output) => { 5 | expect(Math.sqrt(input)).toBe(output); 6 | }); 7 | 8 | test.concurrent.only.each([ 9 | [4, 2], 10 | [9, 3], 11 | ])("Math.sqrt(%s) = %s", (input, output) => { 12 | expect(Math.sqrt(input)).toBe(output); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-concurrent-skip-each.input.js: -------------------------------------------------------------------------------- 1 | it.concurrent.skip.each([ 2 | [4, 3], 3 | [9, 4], 4 | ])("Math.sqrt(%s) = %s", (input, output) => { 5 | expect(Math.sqrt(input)).toBe(output); 6 | }); 7 | 8 | test.concurrent.skip.each([ 9 | [4, 3], 10 | [9, 4], 11 | ])("Math.sqrt(%s) = %s", (input, output) => { 12 | expect(Math.sqrt(input)).toBe(output); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-each.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.each([ 3 | [4, 2], 4 | [9, 3], 5 | ])("Math.sqrt(%s) = %s", (input, output) => { 6 | expect(Math.sqrt(input)).toBe(output); 7 | }); 8 | 9 | test.each([ 10 | [4, 2], 11 | [9, 3], 12 | ])("Math.sqrt(%s) = %s", (input, output) => { 13 | expect(Math.sqrt(input)).toBe(output); 14 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toHaveBeenNthCalledWith.input.js: -------------------------------------------------------------------------------- 1 | test("toHaveBeenNthCalledWith", () => { 2 | const mockFn = jest.fn(); 3 | 4 | mockFn(); 5 | expect(mockFn).toHaveBeenNthCalledWith(1); 6 | 7 | mockFn("foo"); 8 | expect(mockFn).toHaveBeenNthCalledWith(2, "foo"); 9 | 10 | mockFn("foo", "bar"); 11 | expect(mockFn).toHaveBeenNthCalledWith(3, "foo", "bar"); 12 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toStrictEqual.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | class Stock { 3 | constructor(type) { 4 | this.type = type; 5 | } 6 | } 7 | 8 | test("structurally the same, but semantically different", () => { 9 | expect(new Stock("apples")).toEqual({ type: "apples" }); 10 | expect(new Stock("apples")).not.toStrictEqual({ type: "apples" }); 11 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/describe/describe-only-each.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe.only.each([ 3 | [4, 2], 4 | [9, 3], 5 | ])("Math.sqrt(%s)", (input, output) => { 6 | test(`returns ${output}`, () => { 7 | expect(Math.sqrt(input)).toBe(output); 8 | }); 9 | }); 10 | 11 | test('will not be run', () => { 12 | expect(Math.sqrt(4)).toBe(3); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/misc/with-file-headers.output.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Example comment which should not be replaced. 3 | */ 4 | 5 | import { describe, expect, test } from "vitest"; 6 | 7 | describe("basic", () => { 8 | test("Math.sqrt()", () => { 9 | expect(Math.sqrt(4)).toBe(2); 10 | expect(Math.sqrt(144)).toBe(12); 11 | expect(Math.sqrt(2)).toBe(Math.SQRT2); 12 | }) 13 | }); 14 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mock-default-fn-expr.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockDefault", async () => { 3 | vi.mock("./defaultExport", function() { 4 | return { 5 | default: "defaultFromMock" 6 | }; 7 | }); 8 | 9 | const { default: defaultExport } = await import("./defaultExport"); 10 | expect(defaultExport).toBe("defaultFromMock"); 11 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toHaveReturned.input.js: -------------------------------------------------------------------------------- 1 | test("toHaveReturned", () => { 2 | const mockError = new Error("mockError"); 3 | const mockFn = jest.fn(() => { throw mockError; }); 4 | 5 | try { 6 | mockFn(); 7 | } catch (error) {} 8 | expect(mockFn).not.toHaveReturned(); 9 | 10 | mockFn.mockReturnValue(42); 11 | mockFn(); 12 | expect(mockFn).toHaveReturned(); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/async/rejects.input.js: -------------------------------------------------------------------------------- 1 | describe("rejects", () => { 2 | const mockError = new Error("error"); 3 | const errorFn = async () => { throw mockError; }; 4 | 5 | test("return", () => { 6 | return expect(errorFn()).rejects.toThrow(mockError); 7 | }); 8 | 9 | test("await", async () => { 10 | await expect(errorFn()).rejects.toThrow(mockError); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/toMatchInlineSnapshot.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe("toMatchInlineSnapshot", () => { 3 | test("string", () => { 4 | expect("foobar".toUpperCase()).toMatchInlineSnapshot(`"FOOBAR"`); 5 | }); 6 | 7 | test("object", () => { 8 | expect({ foo: "bar" }).toMatchInlineSnapshot(`\n{\n "foo": "bar",\n}\n`); 9 | }); 10 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-only-each.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.only.each([ 3 | [4, 2], 4 | [9, 3], 5 | ])("Math.sqrt(%s) = %s", (input, output) => { 6 | expect(Math.sqrt(input)).toBe(output); 7 | }); 8 | 9 | test.only.each([ 10 | [4, 2], 11 | [9, 3], 12 | ])("Math.sqrt(%s) = %s", (input, output) => { 13 | expect(Math.sqrt(input)).toBe(output); 14 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-skip-each.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.skip.each([ 3 | [4, 3], 4 | [9, 4], 5 | ])("Math.sqrt(%s) = %s", (input, output) => { 6 | expect(Math.sqrt(input)).toBe(output); 7 | }); 8 | 9 | test.skip.each([ 10 | [4, 3], 11 | [9, 4], 12 | ])("Math.sqrt(%s) = %s", (input, output) => { 13 | expect(Math.sqrt(input)).toBe(output); 14 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-fails-each.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.fails.each([ 3 | [4, 3], 4 | [9, 4], 5 | ])("Math.sqrt(%s) = %s", (input, output) => { 6 | expect(Math.sqrt(input)).toBe(output); 7 | }); 8 | 9 | test.fails.each([ 10 | [4, 3], 11 | [9, 4], 12 | ])("Math.sqrt(%s) = %s", (input, output) => { 13 | expect(Math.sqrt(input)).toBe(output); 14 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toHaveBeenCalledWith.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toHaveBeenCalledWith", () => { 3 | const mockFn = vi.fn(); 4 | 5 | mockFn(); 6 | mockFn("foo"); 7 | mockFn("foo", "bar"); 8 | 9 | expect(mockFn).toHaveBeenCalledWith(); 10 | expect(mockFn).toHaveBeenCalledWith("foo"); 11 | expect(mockFn).toHaveBeenCalledWith("foo", "bar"); 12 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/async/resolves.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe("resolves", () => { 3 | const mockData = "data"; 4 | const dataFn = async () => mockData; 5 | 6 | test("return", () => { 7 | return expect(dataFn()).resolves.toBe(mockData); 8 | }); 9 | 10 | test("await", async () => { 11 | await expect(dataFn()).resolves.toBe(mockData); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toReturn.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toReturn", () => { 3 | const mockError = new Error("mockError"); 4 | const mockFn = vi.fn(() => { throw mockError; }); 5 | 6 | try { 7 | mockFn(); 8 | } catch (error) {} 9 | expect(mockFn).not.toReturn(); 10 | 11 | mockFn.mockReturnValue(42); 12 | mockFn(); 13 | expect(mockFn).toReturn(); 14 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-concurrent-each.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.concurrent.each([ 3 | [4, 2], 4 | [9, 3], 5 | ])("Math.sqrt(%s) = %s", (input, output) => { 6 | expect(Math.sqrt(input)).toBe(output); 7 | }); 8 | 9 | test.concurrent.each([ 10 | [4, 2], 11 | [9, 3], 12 | ])("Math.sqrt(%s) = %s", (input, output) => { 13 | expect(Math.sqrt(input)).toBe(output); 14 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/calls.input.js: -------------------------------------------------------------------------------- 1 | test("calls", () => { 2 | const mockFn = jest.fn(); 3 | expect(mockFn.mock.calls).toHaveLength(0); 4 | 5 | mockFn("foo"); 6 | expect(mockFn.mock.calls).toHaveLength(1); 7 | expect(mockFn.mock.calls[0]).toEqual(["foo"]); 8 | 9 | mockFn("bar", "baz"); 10 | expect(mockFn.mock.calls).toHaveLength(2); 11 | expect(mockFn.mock.calls[1]).toEqual(["bar", "baz"]); 12 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/misc/with-existing-imports.output.mjs: -------------------------------------------------------------------------------- 1 | /* 2 | * Example comment which should not be replaced. 3 | */ 4 | import { describe, test } from "vitest"; 5 | 6 | import assert from 'node:assert'; 7 | 8 | describe("basic", () => { 9 | test("Math.sqrt()", () => { 10 | assert(Math.sqrt(4) === 2); 11 | assert(Math.sqrt(144) === 12); 12 | assert(Math.sqrt(2) === Math.SQRT2); 13 | }) 14 | }) 15 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toHaveBeenLastCalledWith.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toHaveBeenLastCalledWith", () => { 3 | const mockFn = vi.fn(); 4 | 5 | mockFn(); 6 | expect(mockFn).toHaveBeenLastCalledWith(); 7 | 8 | mockFn("foo"); 9 | expect(mockFn).toHaveBeenLastCalledWith("foo"); 10 | 11 | mockFn("foo", "bar"); 12 | expect(mockFn).toHaveBeenLastCalledWith("foo", "bar"); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/toHaveBeenNthCalledWith.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toHaveBeenNthCalledWith", () => { 3 | const mockFn = vi.fn(); 4 | 5 | mockFn(); 6 | expect(mockFn).toHaveBeenNthCalledWith(1); 7 | 8 | mockFn("foo"); 9 | expect(mockFn).toHaveBeenNthCalledWith(2, "foo"); 10 | 11 | mockFn("foo", "bar"); 12 | expect(mockFn).toHaveBeenNthCalledWith(3, "foo", "bar"); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/instances.input.js: -------------------------------------------------------------------------------- 1 | test("instances", () => { 2 | const mockFn = jest.fn(); 3 | expect(mockFn.mock.instances).toHaveLength(0); 4 | 5 | const a = new mockFn(); 6 | expect(mockFn.mock.instances).toHaveLength(1); 7 | expect(mockFn.mock.instances[0]).toBe(a); 8 | 9 | const b = new mockFn(); 10 | expect(mockFn.mock.instances).toHaveLength(2); 11 | expect(mockFn.mock.instances[1]).toBe(b); 12 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-concurrent-only-each.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.concurrent.only.each([ 3 | [4, 2], 4 | [9, 3], 5 | ])("Math.sqrt(%s) = %s", (input, output) => { 6 | expect(Math.sqrt(input)).toBe(output); 7 | }); 8 | 9 | test.concurrent.only.each([ 10 | [4, 2], 11 | [9, 3], 12 | ])("Math.sqrt(%s) = %s", (input, output) => { 13 | expect(Math.sqrt(input)).toBe(output); 14 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/test/test-concurrent-skip-each.output.js: -------------------------------------------------------------------------------- 1 | import { expect, it, test } from "vitest"; 2 | it.concurrent.skip.each([ 3 | [4, 3], 4 | [9, 4], 5 | ])("Math.sqrt(%s) = %s", (input, output) => { 6 | expect(Math.sqrt(input)).toBe(output); 7 | }); 8 | 9 | test.concurrent.skip.each([ 10 | [4, 3], 11 | [9, 4], 12 | ])("Math.sqrt(%s) = %s", (input, output) => { 13 | expect(Math.sqrt(input)).toBe(output); 14 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/misc/with-top-jsdoc-comment.input.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Returns the square root of a number. 3 | * @param {number} val 4 | * @returns Square root of val 5 | */ 6 | function sqrt(val) { 7 | return Math.sqrt(val); 8 | } 9 | 10 | describe("basic", () => { 11 | test("Math.sqrt()", () => { 12 | expect(sqrt(4)).toBe(2); 13 | expect(sqrt(144)).toBe(12); 14 | expect(sqrt(2)).toBe(Math.SQRT2); 15 | }) 16 | }) 17 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toHaveReturned.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toHaveReturned", () => { 3 | const mockError = new Error("mockError"); 4 | const mockFn = vi.fn(() => { throw mockError; }); 5 | 6 | try { 7 | mockFn(); 8 | } catch (error) {} 9 | expect(mockFn).not.toHaveReturned(); 10 | 11 | mockFn.mockReturnValue(42); 12 | mockFn(); 13 | expect(mockFn).toHaveReturned(); 14 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/assertions.input.js: -------------------------------------------------------------------------------- 1 | async function doAsync(...cbs) { 2 | await Promise.all(cbs.map((cb, index) => cb({ index }))); 3 | } 4 | 5 | test("all assertions are called", async () => { 6 | expect.assertions(2); 7 | function callback1(data) { 8 | expect(data).toBeTruthy(); 9 | } 10 | function callback2(data) { 11 | expect(data).toBeTruthy(); 12 | } 13 | 14 | await doAsync(callback1, callback2); 15 | }); 16 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/async/rejects.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe("rejects", () => { 3 | const mockError = new Error("error"); 4 | const errorFn = async () => { throw mockError; }; 5 | 6 | test("return", () => { 7 | return expect(errorFn()).rejects.toThrow(mockError); 8 | }); 9 | 10 | test("await", async () => { 11 | await expect(errorFn()).rejects.toThrow(mockError); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toReturnTimes.input.js: -------------------------------------------------------------------------------- 1 | test("toReturnTimes", () => { 2 | const mockError = new Error("mockError"); 3 | const mockFn = jest.fn(() => { throw mockError; }); 4 | 5 | try { 6 | mockFn(); 7 | } catch (error) {} 8 | expect(mockFn).toReturnTimes(0); 9 | 10 | mockFn.mockReturnValue(42); 11 | 12 | mockFn(); 13 | expect(mockFn).toReturnTimes(1); 14 | mockFn(); 15 | expect(mockFn).toReturnTimes(2); 16 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/resetModules.input.js: -------------------------------------------------------------------------------- 1 | test("resetModules", async () => { 2 | const { getToken: getToken1 } = await import("./getToken"); 3 | const { getToken: getToken2 } = await import("./getToken"); 4 | expect(getToken1).toBe(getToken2); 5 | 6 | jest.resetModules(); 7 | 8 | const { getToken: getToken3 } = await import("./getToken"); 9 | expect(getToken1).not.toBe(getToken3); 10 | expect(getToken2).not.toBe(getToken3); 11 | }); 12 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/calls/calls.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("calls", () => { 3 | const mockFn = vi.fn(); 4 | expect(mockFn.mock.calls).toHaveLength(0); 5 | 6 | mockFn("foo"); 7 | expect(mockFn.mock.calls).toHaveLength(1); 8 | expect(mockFn.mock.calls[0]).toEqual(["foo"]); 9 | 10 | mockFn("bar", "baz"); 11 | expect(mockFn.mock.calls).toHaveLength(2); 12 | expect(mockFn.mock.calls[1]).toEqual(["bar", "baz"]); 13 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeCloseTo.input.js: -------------------------------------------------------------------------------- 1 | test.failing("decimals are not equal in javascript", () => { 2 | expect(0.2 + 0.1).toBe(0.3); // 0.2 + 0.1 is 0.30000000000000004 3 | }); 4 | 5 | test("decimals are rounded to 5 after the point", () => { 6 | // 0.2 + 0.1 is 0.30000 | "000000000004" removed 7 | expect(0.2 + 0.1).toBeCloseTo(0.3, 5); 8 | // nothing from 0.30000000000000004 is removed 9 | expect(0.2 + 0.1).not.toBeCloseTo(0.3, 50); 10 | }); 11 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/instances.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("instances", () => { 3 | const mockFn = vi.fn(); 4 | expect(mockFn.mock.instances).toHaveLength(0); 5 | 6 | const a = new mockFn(); 7 | expect(mockFn.mock.instances).toHaveLength(1); 8 | expect(mockFn.mock.instances[0]).toBe(a); 9 | 10 | const b = new mockFn(); 11 | expect(mockFn.mock.instances).toHaveLength(2); 12 | expect(mockFn.mock.instances[1]).toBe(b); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toHaveReturnedTimes.input.js: -------------------------------------------------------------------------------- 1 | test("toHaveReturnedTimes", () => { 2 | const mockError = new Error("mockError"); 3 | const mockFn = jest.fn(() => { throw mockError; }); 4 | 5 | try { 6 | mockFn(); 7 | } catch (error) {} 8 | expect(mockFn).toHaveReturnedTimes(0); 9 | 10 | mockFn.mockReturnValue(42); 11 | 12 | mockFn(); 13 | expect(mockFn).toHaveReturnedTimes(1); 14 | mockFn(); 15 | expect(mockFn).toHaveReturnedTimes(2); 16 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/toMatchSnapshot.input.js: -------------------------------------------------------------------------------- 1 | describe("toMatchSnapshot", () => { 2 | test("string", () => { 3 | expect("foobar".toUpperCase()).toMatchSnapshot(); 4 | }); 5 | 6 | test("object", () => { 7 | expect({ 8 | createdAt: new Date(), 9 | id: Math.floor(Math.random() * 20), 10 | name: "John Doe" 11 | }).toMatchSnapshot({ 12 | createdAt: expect.any(Date), 13 | id: expect.any(Number), 14 | }); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/assertions.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | async function doAsync(...cbs) { 3 | await Promise.all(cbs.map((cb, index) => cb({ index }))); 4 | } 5 | 6 | test("all assertions are called", async () => { 7 | expect.assertions(2); 8 | function callback1(data) { 9 | expect(data).toBeTruthy(); 10 | } 11 | function callback2(data) { 12 | expect(data).toBeTruthy(); 13 | } 14 | 15 | await doAsync(callback1, callback2); 16 | }); 17 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/misc/with-top-jsdoc-comment.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | /** 3 | * Returns the square root of a number. 4 | * @param {number} val 5 | * @returns Square root of val 6 | */ 7 | function sqrt(val) { 8 | return Math.sqrt(val); 9 | } 10 | 11 | describe("basic", () => { 12 | test("Math.sqrt()", () => { 13 | expect(sqrt(4)).toBe(2); 14 | expect(sqrt(144)).toBe(12); 15 | expect(sqrt(2)).toBe(Math.SQRT2); 16 | }) 17 | }) 18 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/mockRejectedValue.input.js: -------------------------------------------------------------------------------- 1 | test("mockRejectedValue", () => { 2 | const originalRejectMsg = "originalRejectMsg"; 3 | const updatedRejectMsg = "updatedRejectMsg"; 4 | 5 | const mockFn = jest.fn(() => Promise.reject(originalRejectMsg)); 6 | expect(mockFn()).rejects.toBe(originalRejectMsg); 7 | 8 | mockFn.mockRejectedValue(updatedRejectMsg); 9 | expect(mockFn()).rejects.toBe(updatedRejectMsg); 10 | expect(mockFn()).rejects.toBe(updatedRejectMsg); 11 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toReturnTimes.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toReturnTimes", () => { 3 | const mockError = new Error("mockError"); 4 | const mockFn = vi.fn(() => { throw mockError; }); 5 | 6 | try { 7 | mockFn(); 8 | } catch (error) {} 9 | expect(mockFn).toReturnTimes(0); 10 | 11 | mockFn.mockReturnValue(42); 12 | 13 | mockFn(); 14 | expect(mockFn).toReturnTimes(1); 15 | mockFn(); 16 | expect(mockFn).toReturnTimes(2); 17 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/resetModules.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("resetModules", async () => { 3 | const { getToken: getToken1 } = await import("./getToken"); 4 | const { getToken: getToken2 } = await import("./getToken"); 5 | expect(getToken1).toBe(getToken2); 6 | 7 | vi.resetModules(); 8 | 9 | const { getToken: getToken3 } = await import("./getToken"); 10 | expect(getToken1).not.toBe(getToken3); 11 | expect(getToken2).not.toBe(getToken3); 12 | }); 13 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toBeCloseTo.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test.fails("decimals are not equal in javascript", () => { 3 | expect(0.2 + 0.1).toBe(0.3); // 0.2 + 0.1 is 0.30000000000000004 4 | }); 5 | 6 | test("decimals are rounded to 5 after the point", () => { 7 | // 0.2 + 0.1 is 0.30000 | "000000000004" removed 8 | expect(0.2 + 0.1).toBeCloseTo(0.3, 5); 9 | // nothing from 0.30000000000000004 is removed 10 | expect(0.2 + 0.1).not.toBeCloseTo(0.3, 50); 11 | }); 12 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/mockRejectedValueOnce.input.js: -------------------------------------------------------------------------------- 1 | test("mockRejectedValueOnce", () => { 2 | const originalRejectMsg = "originalRejectMsg"; 3 | const updatedRejectMsg = "updatedRejectMsg"; 4 | 5 | const mockFn = jest.fn(() => Promise.reject(originalRejectMsg)); 6 | expect(mockFn()).rejects.toBe(originalRejectMsg); 7 | 8 | mockFn.mockRejectedValueOnce(updatedRejectMsg); 9 | expect(mockFn()).rejects.toBe(updatedRejectMsg); 10 | 11 | expect(mockFn()).rejects.toBe(originalRejectMsg); 12 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockRestore.input.js: -------------------------------------------------------------------------------- 1 | test("mockRestore", () => { 2 | const originalReturnValue = "original"; 3 | const mockReturnValue = "mocked"; 4 | 5 | const module = { api: () => originalReturnValue }; 6 | jest.spyOn(module, "api").mockImplementation(() => mockReturnValue); 7 | 8 | expect(module.api()).toStrictEqual(mockReturnValue); 9 | expect(module.api).toHaveBeenCalledTimes(1); 10 | 11 | module.api.mockRestore(); 12 | expect(module.api()).toStrictEqual(originalReturnValue); 13 | }); 14 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | workflow_call: 5 | 6 | jobs: 7 | build: 8 | runs-on: ubuntu-latest 9 | 10 | strategy: 11 | matrix: 12 | node-version: [14.x, 16.x, 18.x] 13 | 14 | steps: 15 | - uses: actions/checkout@v3 16 | - uses: actions/setup-node@v3 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | cache: "yarn" 20 | - run: yarn 21 | - run: yarn lint 22 | - run: yarn build 23 | - run: yarn test 24 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/toHaveReturnedTimes.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("toHaveReturnedTimes", () => { 3 | const mockError = new Error("mockError"); 4 | const mockFn = vi.fn(() => { throw mockError; }); 5 | 6 | try { 7 | mockFn(); 8 | } catch (error) {} 9 | expect(mockFn).toHaveReturnedTimes(0); 10 | 11 | mockFn.mockReturnValue(42); 12 | 13 | mockFn(); 14 | expect(mockFn).toHaveReturnedTimes(1); 15 | mockFn(); 16 | expect(mockFn).toHaveReturnedTimes(2); 17 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/toMatchSnapshot.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe("toMatchSnapshot", () => { 3 | test("string", () => { 4 | expect("foobar".toUpperCase()).toMatchSnapshot(); 5 | }); 6 | 7 | test("object", () => { 8 | expect({ 9 | createdAt: new Date(), 10 | id: Math.floor(Math.random() * 20), 11 | name: "John Doe" 12 | }).toMatchSnapshot({ 13 | createdAt: expect.any(Date), 14 | id: expect.any(Number), 15 | }); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vitest-codemod 2 | 3 | This repository contains a collection of codemod scripts for use with 4 | [JSCodeshift][jscodeshift] that help migrate to [vitest][vitest]. 5 | 6 | The `vitest-codemod` CLI is a lightweight wrapper over jscodeshift. 7 | It processes `--help`, `--version` and `--transform` options before passing them 8 | downstream. 9 | 10 | Check details in [packages/vitest-codemod/README.md](packages/vitest-codemod/README.md). 11 | 12 | [jscodeshift]: https://github.com/facebook/jscodeshift 13 | [vitest]: https://vitest.dev/ 14 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/mockRejectedValue.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockRejectedValue", () => { 3 | const originalRejectMsg = "originalRejectMsg"; 4 | const updatedRejectMsg = "updatedRejectMsg"; 5 | 6 | const mockFn = vi.fn(() => Promise.reject(originalRejectMsg)); 7 | expect(mockFn()).rejects.toBe(originalRejectMsg); 8 | 9 | mockFn.mockRejectedValue(updatedRejectMsg); 10 | expect(mockFn()).rejects.toBe(updatedRejectMsg); 11 | expect(mockFn()).rejects.toBe(updatedRejectMsg); 12 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/mockRejectedValueOnce.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockRejectedValueOnce", () => { 3 | const originalRejectMsg = "originalRejectMsg"; 4 | const updatedRejectMsg = "updatedRejectMsg"; 5 | 6 | const mockFn = vi.fn(() => Promise.reject(originalRejectMsg)); 7 | expect(mockFn()).rejects.toBe(originalRejectMsg); 8 | 9 | mockFn.mockRejectedValueOnce(updatedRejectMsg); 10 | expect(mockFn()).rejects.toBe(updatedRejectMsg); 11 | 12 | expect(mockFn()).rejects.toBe(originalRejectMsg); 13 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockRestore.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockRestore", () => { 3 | const originalReturnValue = "original"; 4 | const mockReturnValue = "mocked"; 5 | 6 | const module = { api: () => originalReturnValue }; 7 | vi.spyOn(module, "api").mockImplementation(() => mockReturnValue); 8 | 9 | expect(module.api()).toStrictEqual(mockReturnValue); 10 | expect(module.api).toHaveBeenCalledTimes(1); 11 | 12 | module.api.mockRestore(); 13 | expect(module.api()).toStrictEqual(originalReturnValue); 14 | }); 15 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/async/await.input.js: -------------------------------------------------------------------------------- 1 | describe("await", () => { 2 | test("data", async () => { 3 | const mockData = "data"; 4 | const dataFn = async () => mockData; 5 | 6 | const data = await dataFn(); 7 | expect(data).toBe(mockData); 8 | }); 9 | 10 | test("error", async () => { 11 | expect.assertions(1); 12 | const mockError = new Error("error"); 13 | const errorFn = async () => { throw mockError; }; 14 | 15 | try { 16 | await errorFn(); 17 | } catch(error) { 18 | expect(error).toBe(mockError); 19 | } 20 | }); 21 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockReturnThis.input.js: -------------------------------------------------------------------------------- 1 | class Car { 2 | constructor(model) { 3 | this.model = model; 4 | } 5 | 6 | start() { 7 | return this; 8 | } 9 | 10 | drive() { 11 | return this; 12 | } 13 | } 14 | 15 | test("mockReturnThis", () => { 16 | const car = new Car("Honda"); 17 | 18 | const startSpy = jest.spyOn(car, 'start').mockReturnThis(); 19 | const driveSpy = jest.spyOn(car, 'drive').mockReturnThis(); 20 | 21 | expect(car.start().drive()).toBe(car); 22 | expect(startSpy).toHaveBeenCalledTimes(1); 23 | expect(driveSpy).toHaveBeenCalledTimes(1); 24 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/async/await.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe("await", () => { 3 | test("data", async () => { 4 | const mockData = "data"; 5 | const dataFn = async () => mockData; 6 | 7 | const data = await dataFn(); 8 | expect(data).toBe(mockData); 9 | }); 10 | 11 | test("error", async () => { 12 | expect.assertions(1); 13 | const mockError = new Error("error"); 14 | const errorFn = async () => { throw mockError; }; 15 | 16 | try { 17 | await errorFn(); 18 | } catch(error) { 19 | expect(error).toBe(mockError); 20 | } 21 | }); 22 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockReturnThis.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | class Car { 3 | constructor(model) { 4 | this.model = model; 5 | } 6 | 7 | start() { 8 | return this; 9 | } 10 | 11 | drive() { 12 | return this; 13 | } 14 | } 15 | 16 | test("mockReturnThis", () => { 17 | const car = new Car("Honda"); 18 | 19 | const startSpy = vi.spyOn(car, 'start').mockReturnThis(); 20 | const driveSpy = vi.spyOn(car, 'drive').mockReturnThis(); 21 | 22 | expect(car.start().drive()).toBe(car); 23 | expect(startSpy).toHaveBeenCalledTimes(1); 24 | expect(driveSpy).toHaveBeenCalledTimes(1); 25 | }) -------------------------------------------------------------------------------- /packages/jest/src/apis/getApisFromCallExpression.ts: -------------------------------------------------------------------------------- 1 | import type { Collection, JSCodeshift } from 'jscodeshift' 2 | 3 | const jestGlobalApis = ['afterAll', 'afterEach', 'beforeAll', 'beforeEach', 'describe', 'test', 'it', 'fit', 'expect'] 4 | 5 | export const getApisFromCallExpression = (j: JSCodeshift, source: Collection): string[] => { 6 | const apisFromCallExpression = [] 7 | 8 | for (const jestGlobalApi of jestGlobalApis) { 9 | const calls = source.find(j.CallExpression, { callee: { name: jestGlobalApi } }) 10 | 11 | if (calls.length > 0) 12 | apisFromCallExpression.push(jestGlobalApi !== 'fit' ? jestGlobalApi : 'it') 13 | } 14 | 15 | return apisFromCallExpression 16 | } 17 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockClear.input.js: -------------------------------------------------------------------------------- 1 | test("mockClear", () => { 2 | const mockFn = jest.fn(); 3 | 4 | mockFn("foo"); 5 | expect(mockFn.mock.calls).toHaveLength(1); 6 | expect(mockFn.mock.instances).toHaveLength(1); 7 | expect(mockFn.mock.results).toHaveLength(1); 8 | // ToDo: https://github.com/vitest-dev/vitest/issues/2810 9 | // expect(mockFn.mock.contexts).toHaveLength(1); 10 | 11 | mockFn.mockClear(); 12 | expect(mockFn.mock.calls).toHaveLength(0); 13 | expect(mockFn.mock.instances).toHaveLength(0); 14 | expect(mockFn.mock.results).toHaveLength(0); 15 | // ToDo: https://github.com/vitest-dev/vitest/issues/2810 16 | // expect(mockFn.mock.contexts).toHaveLength(0); 17 | }) -------------------------------------------------------------------------------- /packages/jest/src/apis/addFactoryFunctionToMock.ts: -------------------------------------------------------------------------------- 1 | import type { Collection, JSCodeshift } from 'jscodeshift' 2 | 3 | export const addFactoryFunctionToMock = (j: JSCodeshift, source: Collection) => { 4 | source.find(j.CallExpression, { 5 | callee: { 6 | object: { type: 'Identifier', name: 'jest' }, 7 | property: { type: 'Identifier', name: 'setMock' }, 8 | }, 9 | }).forEach((path) => { 10 | const { arguments: args } = path.value 11 | 12 | if (args.length < 2) 13 | return 14 | 15 | const moduleExport = args[1] 16 | 17 | // @ts-expect-error - moduleExport is usually ObjectExpression 18 | args[1] = j.arrowFunctionExpression([], moduleExport) 19 | }) 20 | } 21 | -------------------------------------------------------------------------------- /packages/types/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@vitest-codemod/types", 3 | "version": "0.0.3", 4 | "description": "Types for vitest-codemod tranforms", 5 | "author": "Kamat, Trivikram ", 6 | "license": "MIT", 7 | "homepage": "https://github.com/trivikr/vitest-codemod/tree/main/packages/types", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/trivikr/vitest-codemod.git", 11 | "directory": "packages/types" 12 | }, 13 | "main": "dist/index.js", 14 | "types": "dist/index.d.ts", 15 | "files": [ 16 | "dist" 17 | ], 18 | "engines": { 19 | "node": ">=14.0.0" 20 | }, 21 | "scripts": { 22 | "build": "yarn g:tsc" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toThrow.input.js: -------------------------------------------------------------------------------- 1 | test("toThrow", () => { 2 | class CustomError extends Error { 3 | constructor(message) { 4 | super(message); 5 | this.name = "CustomError"; 6 | } 7 | } 8 | 9 | function throwFn() { 10 | throw new CustomError("foo bar baz"); 11 | }; 12 | 13 | // Test that the error message says "foo" somewhere: these are equivalent 14 | expect(throwFn).toThrow(/foo/); 15 | expect(throwFn).toThrow("foo"); 16 | 17 | // Test the exact error message 18 | expect(throwFn).toThrow(/^foo bar baz$/); 19 | expect(throwFn).toThrow(new Error("foo bar baz")); 20 | 21 | // Test that we get a CustomError 22 | expect(throwFn).toThrow(CustomError); 23 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toThrowError.input.js: -------------------------------------------------------------------------------- 1 | test("toThrowError", () => { 2 | class CustomError extends Error { 3 | constructor(message) { 4 | super(message); 5 | this.name = "CustomError"; 6 | } 7 | } 8 | 9 | function throwFn() { 10 | throw new CustomError("foo bar baz"); 11 | }; 12 | 13 | // Test that the error message says "foo" somewhere: these are equivalent 14 | expect(throwFn).toThrowError(/foo/); 15 | expect(throwFn).toThrowError("foo"); 16 | 17 | // Test the exact error message 18 | expect(throwFn).toThrowError(/^foo bar baz$/); 19 | expect(throwFn).toThrowError(new Error("foo bar baz")); 20 | 21 | // Test that we get a CustomError 22 | expect(throwFn).toThrowError(CustomError); 23 | }); -------------------------------------------------------------------------------- /.github/workflows/push.yml: -------------------------------------------------------------------------------- 1 | name: push 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | 7 | jobs: 8 | call-build: 9 | uses: ./.github/workflows/build.yml 10 | release: 11 | needs: call-build 12 | runs-on: ubuntu-latest 13 | steps: 14 | - uses: actions/checkout@v3 15 | - uses: actions/setup-node@v3 16 | with: 17 | cache: "yarn" 18 | 19 | - run: yarn 20 | 21 | - name: Create Release Pull Request or Publish to npm 22 | id: changesets 23 | uses: changesets/action@v1 24 | with: 25 | version: yarn version 26 | publish: yarn release 27 | env: 28 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 29 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 30 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/doMock.input.js: -------------------------------------------------------------------------------- 1 | beforeEach(() => { 2 | // resetModules is required to reset mocks in jest. 3 | // In vitest, however, doMock will override the previous mock. 4 | jest.resetModules(); 5 | }); 6 | 7 | test('getToken 1', async () => { 8 | jest.doMock('./getToken', () => ({ 9 | getToken: () => 'token1', 10 | })); 11 | const { getToken } = await import('./getToken'); 12 | expect(getToken()).toBe('token1'); 13 | }); 14 | 15 | test('getToken 2', async () => { 16 | jest.doMock('./getToken', () => ({ 17 | getToken: () => 'token2', 18 | })); 19 | const { getToken } = await import('./getToken'); 20 | // ToDo: https://github.com/vitest-dev/vitest/issues/2870 21 | // expect(getToken()).toBe('token2'); 22 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockClear.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("mockClear", () => { 3 | const mockFn = vi.fn(); 4 | 5 | mockFn("foo"); 6 | expect(mockFn.mock.calls).toHaveLength(1); 7 | expect(mockFn.mock.instances).toHaveLength(1); 8 | expect(mockFn.mock.results).toHaveLength(1); 9 | // ToDo: https://github.com/vitest-dev/vitest/issues/2810 10 | // expect(mockFn.mock.contexts).toHaveLength(1); 11 | 12 | mockFn.mockClear(); 13 | expect(mockFn.mock.calls).toHaveLength(0); 14 | expect(mockFn.mock.instances).toHaveLength(0); 15 | expect(mockFn.mock.results).toHaveLength(0); 16 | // ToDo: https://github.com/vitest-dev/vitest/issues/2810 17 | // expect(mockFn.mock.contexts).toHaveLength(0); 18 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toThrow.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toThrow", () => { 3 | class CustomError extends Error { 4 | constructor(message) { 5 | super(message); 6 | this.name = "CustomError"; 7 | } 8 | } 9 | 10 | function throwFn() { 11 | throw new CustomError("foo bar baz"); 12 | }; 13 | 14 | // Test that the error message says "foo" somewhere: these are equivalent 15 | expect(throwFn).toThrow(/foo/); 16 | expect(throwFn).toThrow("foo"); 17 | 18 | // Test the exact error message 19 | expect(throwFn).toThrow(/^foo bar baz$/); 20 | expect(throwFn).toThrow(new Error("foo bar baz")); 21 | 22 | // Test that we get a CustomError 23 | expect(throwFn).toThrow(CustomError); 24 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toThrowError.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | test("toThrowError", () => { 3 | class CustomError extends Error { 4 | constructor(message) { 5 | super(message); 6 | this.name = "CustomError"; 7 | } 8 | } 9 | 10 | function throwFn() { 11 | throw new CustomError("foo bar baz"); 12 | }; 13 | 14 | // Test that the error message says "foo" somewhere: these are equivalent 15 | expect(throwFn).toThrowError(/foo/); 16 | expect(throwFn).toThrowError("foo"); 17 | 18 | // Test the exact error message 19 | expect(throwFn).toThrowError(/^foo bar baz$/); 20 | expect(throwFn).toThrowError(new Error("foo bar baz")); 21 | 22 | // Test that we get a CustomError 23 | expect(throwFn).toThrowError(CustomError); 24 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/doMock.output.js: -------------------------------------------------------------------------------- 1 | import { beforeEach, expect, test, vi } from "vitest"; 2 | beforeEach(() => { 3 | // resetModules is required to reset mocks in jest. 4 | // In vitest, however, doMock will override the previous mock. 5 | vi.resetModules(); 6 | }); 7 | 8 | test('getToken 1', async () => { 9 | vi.doMock('./getToken', () => ({ 10 | getToken: () => 'token1', 11 | })); 12 | const { getToken } = await import('./getToken'); 13 | expect(getToken()).toBe('token1'); 14 | }); 15 | 16 | test('getToken 2', async () => { 17 | vi.doMock('./getToken', () => ({ 18 | getToken: () => 'token2', 19 | })); 20 | const { getToken } = await import('./getToken'); 21 | // ToDo: https://github.com/vitest-dev/vitest/issues/2870 22 | // expect(getToken()).toBe('token2'); 23 | }); -------------------------------------------------------------------------------- /packages/vitest-codemod/src/utils/getHelpParagraph.ts: -------------------------------------------------------------------------------- 1 | import type { VitestCodemodTransform } from '@vitest-codemod/types' 2 | import { getTransformDescription } from './getTransformDescription' 3 | 4 | const separator = '-'.repeat(95) 5 | 6 | export const getHelpParagraph = (transforms: VitestCodemodTransform[]) => 7 | `${separator} 8 | vitest-codemod is a lightweight wrapper over jscodeshift. 9 | It processes --help, --version and --transform options before passing them downstream. 10 | 11 | You can provide names of the custom transforms instead of a local path or url: 12 | 13 | ${transforms.map(transform => getTransformDescription(transform).join('\n'))} 14 | 15 | Example: vitest-codemod -t jest example.spec.js 16 | 17 | To use the latest version of vitest-codemod, please clear your npx cache and re-run. 18 | 19 | ${separator}\n\n` 20 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/addSnapshotSerializer.input.js: -------------------------------------------------------------------------------- 1 | describe("addSnapshotSerializer", () => { 2 | expect.addSnapshotSerializer({ 3 | serialize(val) { 4 | return JSON.stringify(val); 5 | }, 6 | test(val) { 7 | return val; 8 | } 9 | }); 10 | 11 | describe("toMatchSnapshot", () => { 12 | test("string", () => { 13 | expect("foobar".toUpperCase()).toMatchSnapshot(); 14 | }); 15 | 16 | test("object", () => { 17 | expect({ foo: "bar" }).toMatchSnapshot(); 18 | }); 19 | }); 20 | 21 | describe("toMatchInlineSnapshot", () => { 22 | test("string", () => { 23 | expect("foobar".toUpperCase()).toMatchInlineSnapshot(`"FOOBAR"`); 24 | }); 25 | 26 | test("object", () => { 27 | expect({ foo: "bar" }).toMatchInlineSnapshot(`{"foo":"bar"}`); 28 | }); 29 | }); 30 | }); -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/snapshot/addSnapshotSerializer.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe("addSnapshotSerializer", () => { 3 | expect.addSnapshotSerializer({ 4 | serialize(val) { 5 | return JSON.stringify(val); 6 | }, 7 | test(val) { 8 | return val; 9 | } 10 | }); 11 | 12 | describe("toMatchSnapshot", () => { 13 | test("string", () => { 14 | expect("foobar".toUpperCase()).toMatchSnapshot(); 15 | }); 16 | 17 | test("object", () => { 18 | expect({ foo: "bar" }).toMatchSnapshot(); 19 | }); 20 | }); 21 | 22 | describe("toMatchInlineSnapshot", () => { 23 | test("string", () => { 24 | expect("foobar".toUpperCase()).toMatchInlineSnapshot(`"FOOBAR"`); 25 | }); 26 | 27 | test("object", () => { 28 | expect({ foo: "bar" }).toMatchInlineSnapshot(`{"foo":"bar"}`); 29 | }); 30 | }); 31 | }); -------------------------------------------------------------------------------- /packages/types/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # @vitest-codemod/types 2 | 3 | ## 0.0.3 4 | 5 | ### Patch Changes 6 | 7 | - [#34](https://github.com/trivikr/vitest-codemod/pull/34) [`8e60373`](https://github.com/trivikr/vitest-codemod/commit/8e60373e71a0530b86f73e18a171d249fea76ed7) Thanks [@trivikr](https://github.com/trivikr)! - Format code with @antfu/eslint-config rules 8 | 9 | ## 0.0.2 10 | 11 | ### Patch Changes 12 | 13 | - [#22](https://github.com/trivikr/vitest-codemod/pull/22) [`ca984b4`](https://github.com/trivikr/vitest-codemod/commit/ca984b4cc07349643325bd2d729687e1f00106d9) Thanks [@trivikr](https://github.com/trivikr)! - Remove redundant code 14 | 15 | ## 0.0.1 16 | 17 | ### Patch Changes 18 | 19 | - [#8](https://github.com/trivikr/vitest-codemod/pull/8) [`8dbfa21`](https://github.com/trivikr/vitest-codemod/commit/8dbfa218adf6ff042cb25928341781d2a79448fd) Thanks [@trivikr](https://github.com/trivikr)! - Add source code for @vitest-codemod/types 20 | -------------------------------------------------------------------------------- /packages/types/src/index.ts: -------------------------------------------------------------------------------- 1 | export type PositionalOptionsType = 'boolean' | 'number' | 'string' 2 | 3 | export interface VitestCodemodTransformOption { 4 | /** array of values, limit valid option arguments to a predefined set */ 5 | choices?: readonly boolean[] | readonly number[] | readonly string[] 6 | 7 | /** value, set a default value for the option */ 8 | default?: boolean | number | string 9 | 10 | /** string, the option description for help content */ 11 | description: string 12 | 13 | /** string, the type of the option */ 14 | type: PositionalOptionsType 15 | } 16 | 17 | export interface VitestCodemodTransform { 18 | /** string, the name of the transform */ 19 | name: string 20 | 21 | /** string, the description of the transform for help content */ 22 | description: string 23 | 24 | /** array of TransformOption, the which can be passed to the transform */ 25 | options?: VitestCodemodTransformOption[] 26 | } 27 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/async/promises.input.js: -------------------------------------------------------------------------------- 1 | describe("promises", () => { 2 | describe("data", () => { 3 | const mockData = "data"; 4 | const dataFn = async () => mockData; 5 | 6 | test("return", () => { 7 | return dataFn().then((data) => { 8 | expect(data).toBe(mockData); 9 | }); 10 | }); 11 | 12 | test("await", async () => { 13 | await dataFn().then((data) => { 14 | expect(data).toBe(mockData); 15 | }); 16 | }); 17 | }); 18 | 19 | describe("error", () => { 20 | const mockError = new Error("error"); 21 | const errorFn = async () => { throw mockError; }; 22 | 23 | test("return", () => { 24 | return errorFn().catch((error) => { 25 | expect(error).toBe(mockError); 26 | }); 27 | }); 28 | 29 | test("await", async () => { 30 | await errorFn().catch((error) => { 31 | expect(error).toBe(mockError); 32 | }); 33 | }); 34 | }); 35 | }); -------------------------------------------------------------------------------- /packages/jest/src/apis/replaceTestApiFit.ts: -------------------------------------------------------------------------------- 1 | import type { Collection, JSCodeshift } from 'jscodeshift' 2 | 3 | export const replaceTestApiFit = (j: JSCodeshift, source: Collection): void => { 4 | const jestApiName = 'fit' 5 | const vitestApiObject = j.memberExpression(j.identifier('it'), j.identifier('only')) 6 | 7 | // Replace `fit` with `it.only` 8 | source.find(j.CallExpression, { 9 | callee: { type: 'Identifier', name: jestApiName }, 10 | }).forEach((path) => { 11 | path.node.callee = vitestApiObject 12 | return path 13 | }) 14 | 15 | // Replace `fit.(each|failing)` with `it.only.(each|failing)` 16 | for (const fitModifierName of ['each', 'failing']) { 17 | source.find(j.MemberExpression, { 18 | object: { type: 'Identifier', name: jestApiName }, 19 | property: { type: 'Identifier', name: fitModifierName }, 20 | }).forEach((path) => { 21 | path.node.object = vitestApiObject 22 | return path 23 | }) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/async/promises.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test } from "vitest"; 2 | describe("promises", () => { 3 | describe("data", () => { 4 | const mockData = "data"; 5 | const dataFn = async () => mockData; 6 | 7 | test("return", () => { 8 | return dataFn().then((data) => { 9 | expect(data).toBe(mockData); 10 | }); 11 | }); 12 | 13 | test("await", async () => { 14 | await dataFn().then((data) => { 15 | expect(data).toBe(mockData); 16 | }); 17 | }); 18 | }); 19 | 20 | describe("error", () => { 21 | const mockError = new Error("error"); 22 | const errorFn = async () => { throw mockError; }; 23 | 24 | test("return", () => { 25 | return errorFn().catch((error) => { 26 | expect(error).toBe(mockError); 27 | }); 28 | }); 29 | 30 | test("await", async () => { 31 | await errorFn().catch((error) => { 32 | expect(error).toBe(mockError); 33 | }); 34 | }); 35 | }); 36 | }); -------------------------------------------------------------------------------- /packages/jest/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@vitest-codemod/jest", 3 | "version": "0.4.5", 4 | "description": "Transformer for migrating from Jest to Vitest", 5 | "author": "Kamat, Trivikram ", 6 | "license": "MIT", 7 | "homepage": "https://github.com/trivikr/vitest-codemod/tree/main/packages/jest", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/trivikr/vitest-codemod.git", 11 | "directory": "packages/jest" 12 | }, 13 | "main": "dist/index.js", 14 | "types": "dist/index.d.ts", 15 | "files": [ 16 | "dist" 17 | ], 18 | "engines": { 19 | "node": ">=14.0.0" 20 | }, 21 | "scripts": { 22 | "build": "yarn g:tsc", 23 | "test:jest": "yarn node --experimental-vm-modules $(yarn bin jest)", 24 | "test": "yarn test:jest && yarn g:vitest" 25 | }, 26 | "dependencies": { 27 | "jscodeshift": "0.14.0" 28 | }, 29 | "devDependencies": { 30 | "@types/jest": "^29.4.0", 31 | "@vitest-codemod/types": "^0.0.3", 32 | "jest": "^29.4.3" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /packages/vitest-codemod/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vitest-codemod", 3 | "version": "0.1.3", 4 | "description": "Codemod scripts to migrate your JavaScript unit tests to vitest", 5 | "author": "Kamat, Trivikram ", 6 | "license": "MIT", 7 | "homepage": "https://github.com/trivikr/vitest-codemod/tree/main/packages/vitest-codemod", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/trivikr/vitest-codemod.git", 11 | "directory": "packages/vitest-codemod" 12 | }, 13 | "keywords": [ 14 | "jscodeshift", 15 | "testing", 16 | "codemod", 17 | "jest", 18 | "vitest", 19 | "migration" 20 | ], 21 | "main": "dist/cli.js", 22 | "types": "dist/cli.d.ts", 23 | "bin": "bin/vitest-codemod", 24 | "files": [ 25 | "bin", 26 | "dist" 27 | ], 28 | "engines": { 29 | "node": ">=14.0.0" 30 | }, 31 | "scripts": { 32 | "build": "yarn g:tsc" 33 | }, 34 | "dependencies": { 35 | "@vitest-codemod/jest": "^0.4.0", 36 | "jscodeshift": "0.14.0" 37 | }, 38 | "devDependencies": { 39 | "@vitest-codemod/types": "^0.0.3" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Trivikram Kamat 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockReset.input.js: -------------------------------------------------------------------------------- 1 | describe("mockReset", () => { 2 | const originalReturnValue = "original"; 3 | const mockReturnValue = "mocked"; 4 | 5 | test("spyOn", () => { 6 | const module = { api: () => originalReturnValue }; 7 | jest.spyOn(module, "api").mockImplementation(() => mockReturnValue); 8 | 9 | expect(module.api()).toStrictEqual(mockReturnValue); 10 | expect(module.api).toHaveBeenCalledTimes(1); 11 | 12 | module.api.mockReset(); 13 | expect(module.api).toHaveBeenCalledTimes(0); 14 | 15 | // Bug in jest: https://github.com/facebook/jest/issues/13916 16 | // expect(module.api()).toStrictEqual(undefined); 17 | }); 18 | 19 | test("overwrite", () => { 20 | const module = { api: () => originalReturnValue }; 21 | module.api = jest.fn().mockImplementation(() => mockReturnValue); 22 | 23 | expect(module.api()).toStrictEqual(mockReturnValue); 24 | expect(module.api).toHaveBeenCalledTimes(1); 25 | 26 | module.api.mockReset(); 27 | expect(module.api).toHaveBeenCalledTimes(0); 28 | 29 | expect(module.api()).toStrictEqual(undefined); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/spyOn.input.js: -------------------------------------------------------------------------------- 1 | describe("spyOn", () => { 2 | test("method", () => { 3 | const video = { 4 | play() { 5 | return true; 6 | }, 7 | }; 8 | 9 | const spy = jest.spyOn(video, "play"); 10 | expect(spy).not.toHaveBeenCalled(); 11 | const isPlaying = video.play(); 12 | 13 | expect(spy).toHaveBeenCalledTimes(1); 14 | expect(isPlaying).toBe(true); 15 | }); 16 | 17 | test("accessType", () => { 18 | const audio = { 19 | _volume: 1, 20 | set volume(value) { 21 | this._volume = value; 22 | }, 23 | get volume() { 24 | return this._volume; 25 | }, 26 | }; 27 | 28 | const setSpy = jest.spyOn(audio, "volume", "set"); 29 | const getSpy = jest.spyOn(audio, "volume", "get"); 30 | 31 | expect(setSpy).not.toHaveBeenCalled(); 32 | expect(getSpy).not.toHaveBeenCalled(); 33 | 34 | audio.volume = 100; 35 | expect(setSpy).toHaveBeenCalledTimes(1); 36 | expect(setSpy).toHaveBeenCalledWith(100); 37 | 38 | const volume = audio.volume; 39 | expect(getSpy).toHaveBeenCalledTimes(1); 40 | expect(volume).toBe(100); 41 | }); 42 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/results.input.js: -------------------------------------------------------------------------------- 1 | test("results", () => { 2 | const mockFn = jest.fn(x => x); 3 | expect(mockFn.mock.results).toHaveLength(0); 4 | 5 | mockFn(); 6 | expect(mockFn.mock.results).toHaveLength(1); 7 | expect(mockFn.mock.results[0]).toEqual({ 8 | type: "return", 9 | value: undefined 10 | }); 11 | 12 | mockFn("foo"); 13 | expect(mockFn.mock.results).toHaveLength(2); 14 | expect(mockFn.mock.results[1]).toEqual({ 15 | type: "return", 16 | value: "foo" 17 | }); 18 | 19 | mockFn({ bar: "baz" }); 20 | expect(mockFn.mock.results).toHaveLength(3); 21 | expect(mockFn.mock.results[2]).toEqual({ 22 | type: "return", 23 | value: { bar: "baz" } 24 | }); 25 | 26 | const mockError = new Error("mockError"); 27 | const mockFnWhichThrows = jest.fn(() => { 28 | throw mockError; 29 | }); 30 | expect(mockFnWhichThrows.mock.results).toHaveLength(0); 31 | 32 | expect(() => { 33 | mockFnWhichThrows(); 34 | }).toThrow(mockError); 35 | expect(mockFnWhichThrows.mock.results).toHaveLength(1); 36 | expect(mockFnWhichThrows.mock.results[0]).toEqual({ 37 | type: "throw", 38 | value: mockError 39 | }); 40 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/mockReset.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test, vi } from "vitest"; 2 | describe("mockReset", () => { 3 | const originalReturnValue = "original"; 4 | const mockReturnValue = "mocked"; 5 | 6 | test("spyOn", () => { 7 | const module = { api: () => originalReturnValue }; 8 | vi.spyOn(module, "api").mockImplementation(() => mockReturnValue); 9 | 10 | expect(module.api()).toStrictEqual(mockReturnValue); 11 | expect(module.api).toHaveBeenCalledTimes(1); 12 | 13 | module.api.mockReset(); 14 | expect(module.api).toHaveBeenCalledTimes(0); 15 | 16 | // Bug in jest: https://github.com/facebook/jest/issues/13916 17 | // expect(module.api()).toStrictEqual(undefined); 18 | }); 19 | 20 | test("overwrite", () => { 21 | const module = { api: () => originalReturnValue }; 22 | module.api = vi.fn().mockImplementation(() => mockReturnValue); 23 | 24 | expect(module.api()).toStrictEqual(mockReturnValue); 25 | expect(module.api).toHaveBeenCalledTimes(1); 26 | 27 | module.api.mockReset(); 28 | expect(module.api).toHaveBeenCalledTimes(0); 29 | 30 | expect(module.api()).toStrictEqual(undefined); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/mock/spyOn.output.js: -------------------------------------------------------------------------------- 1 | import { describe, expect, test, vi } from "vitest"; 2 | describe("spyOn", () => { 3 | test("method", () => { 4 | const video = { 5 | play() { 6 | return true; 7 | }, 8 | }; 9 | 10 | const spy = vi.spyOn(video, "play"); 11 | expect(spy).not.toHaveBeenCalled(); 12 | const isPlaying = video.play(); 13 | 14 | expect(spy).toHaveBeenCalledTimes(1); 15 | expect(isPlaying).toBe(true); 16 | }); 17 | 18 | test("accessType", () => { 19 | const audio = { 20 | _volume: 1, 21 | set volume(value) { 22 | this._volume = value; 23 | }, 24 | get volume() { 25 | return this._volume; 26 | }, 27 | }; 28 | 29 | const setSpy = vi.spyOn(audio, "volume", "set"); 30 | const getSpy = vi.spyOn(audio, "volume", "get"); 31 | 32 | expect(setSpy).not.toHaveBeenCalled(); 33 | expect(getSpy).not.toHaveBeenCalled(); 34 | 35 | audio.volume = 100; 36 | expect(setSpy).toHaveBeenCalledTimes(1); 37 | expect(setSpy).toHaveBeenCalledWith(100); 38 | 39 | const volume = audio.volume; 40 | expect(getSpy).toHaveBeenCalledTimes(1); 41 | expect(volume).toBe(100); 42 | }); 43 | }) -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/returns/results.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test, vi } from "vitest"; 2 | test("results", () => { 3 | const mockFn = vi.fn(x => x); 4 | expect(mockFn.mock.results).toHaveLength(0); 5 | 6 | mockFn(); 7 | expect(mockFn.mock.results).toHaveLength(1); 8 | expect(mockFn.mock.results[0]).toEqual({ 9 | type: "return", 10 | value: undefined 11 | }); 12 | 13 | mockFn("foo"); 14 | expect(mockFn.mock.results).toHaveLength(2); 15 | expect(mockFn.mock.results[1]).toEqual({ 16 | type: "return", 17 | value: "foo" 18 | }); 19 | 20 | mockFn({ bar: "baz" }); 21 | expect(mockFn.mock.results).toHaveLength(3); 22 | expect(mockFn.mock.results[2]).toEqual({ 23 | type: "return", 24 | value: { bar: "baz" } 25 | }); 26 | 27 | const mockError = new Error("mockError"); 28 | const mockFnWhichThrows = vi.fn(() => { 29 | throw mockError; 30 | }); 31 | expect(mockFnWhichThrows.mock.results).toHaveLength(0); 32 | 33 | expect(() => { 34 | mockFnWhichThrows(); 35 | }).toThrow(mockError); 36 | expect(mockFnWhichThrows.mock.results).toHaveLength(1); 37 | expect(mockFnWhichThrows.mock.results[0]).toEqual({ 38 | type: "throw", 39 | value: mockError 40 | }); 41 | }) -------------------------------------------------------------------------------- /.github/workflows/lock.yml: -------------------------------------------------------------------------------- 1 | name: lock-threads 2 | 3 | on: 4 | # 00:00 hours UTC, i.e. 16:00 hours PST or 17:00 hours PDT 5 | schedule: 6 | - cron: "0 0 * * *" 7 | 8 | # allow manual trigger 9 | workflow_dispatch: 10 | 11 | jobs: 12 | lock: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: dessant/lock-threads@v4 16 | if: github.repository == 'trivikr/vitest-codemod' 17 | with: 18 | github-token: ${{ github.token }} 19 | issue-inactive-days: 14 20 | pr-inactive-days: 14 21 | issue-comment: > 22 | This thread has been automatically locked since there has not been 23 | any recent activity after it was closed. Please open a 24 | [new issue](https://github.com/trivikr/vitest-codemod/issues/new/choose) 25 | for related bugs and link to relevant comments in this thread. 26 | pr-comment: > 27 | This thread has been automatically locked since there has not been 28 | any recent activity after it was closed. Please open a 29 | [new issue](https://github.com/trivikr/vitest-codemod/issues/new/choose) 30 | for related bugs and link to relevant comments in this thread. 31 | -------------------------------------------------------------------------------- /packages/jest/src/apis/replaceTestApiFailing.ts: -------------------------------------------------------------------------------- 1 | import type { Collection, Identifier, JSCodeshift } from 'jscodeshift' 2 | 3 | const jestFailsApisName = 'failing' 4 | const vitestFailsApisName = 'fails' 5 | 6 | export const replaceTestApiFailing = (j: JSCodeshift, source: Collection): void => { 7 | for (const testApiName of ['it', 'test']) { 8 | // Replace `(it|test).failing` with `(it|test).fails` 9 | source.find(j.MemberExpression, { 10 | object: { type: 'Identifier', name: testApiName }, 11 | property: { type: 'Identifier', name: jestFailsApisName }, 12 | }).forEach((path) => { 13 | (path.node.property as Identifier).name = vitestFailsApisName 14 | return path 15 | }) 16 | 17 | // Replace `(it|test).(only|skip).failing` with `(it|test).(only|skip).fails` 18 | for (const testApiModifierName of ['only', 'skip']) { 19 | source.find(j.MemberExpression, { 20 | object: { 21 | object: { type: 'Identifier', name: testApiName }, 22 | property: { type: 'Identifier', name: testApiModifierName }, 23 | }, 24 | property: { type: 'Identifier', name: jestFailsApisName }, 25 | }).forEach((path) => { 26 | (path.node.property as Identifier).name = vitestFailsApisName 27 | return path 28 | }) 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /packages/jest/src/modules/prependImport.ts: -------------------------------------------------------------------------------- 1 | import type { Collection, ImportDeclaration, JSCodeshift, Node } from 'jscodeshift' 2 | 3 | export const prependImport = (j: JSCodeshift, source: Collection, importDeclaration: ImportDeclaration) => { 4 | const existingImports = source.find(j.ImportDeclaration) 5 | if (existingImports.length > 0) { 6 | const firstImport = existingImports.at(0) 7 | const firstImportNode = firstImport.nodes()[0] 8 | if (firstImportNode?.comments) { 9 | importDeclaration.comments = firstImportNode.comments 10 | firstImportNode.comments = null 11 | } 12 | 13 | firstImport.insertBefore(importDeclaration) 14 | return 15 | } 16 | 17 | const firstNode: Node = source.find(j.Program).get('body', 0).node 18 | const { comments } = firstNode 19 | if (comments?.length) { 20 | const comment = comments[0] 21 | 22 | // Only move comments that look like file-level comments. Ignore 23 | // line-level and JSDoc-style comments because these probably belong 24 | // to the first node, rather than the file. 25 | if ((comment.type === 'Block' || comment.type === 'CommentBlock') && !comment.value.startsWith('*')) { 26 | importDeclaration.comments = comments 27 | firstNode.comments = null 28 | } 29 | } 30 | 31 | source.get('program', 'body').unshift(importDeclaration) 32 | } 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@vitest-codemod/repository", 3 | "version": "0.0.0", 4 | "private": true, 5 | "packageManager": "yarn@3.4.1", 6 | "description": "Monorepo for vitest-codemod scripts", 7 | "scripts": { 8 | "build": "yarn workspaces foreach --topological-dev --verbose run build", 9 | "g:tsc": "cd $INIT_CWD && tsc -p tsconfig.build.json", 10 | "g:vitest": "cd $INIT_CWD && vitest", 11 | "lint": "eslint 'packages/**/*.{js,ts,json,md}'", 12 | "release": "yarn build && changeset publish", 13 | "test": "yarn workspaces foreach --topological-dev --verbose run test", 14 | "version": "changeset version && yarn --no-immutable" 15 | }, 16 | "devDependencies": { 17 | "@antfu/eslint-config": "^0.35.1", 18 | "@changesets/changelog-github": "^0.4.8", 19 | "@changesets/cli": "^2.26.0", 20 | "@tsconfig/node14": "^1.0.3", 21 | "@types/jscodeshift": "^0.11.6", 22 | "@types/node": "^14.18.36", 23 | "@typescript-eslint/eslint-plugin": "^5.49.0", 24 | "@typescript-eslint/parser": "^5.49.0", 25 | "eslint": "^8.33.0", 26 | "lint-staged": "^13.1.0", 27 | "simple-git-hooks": "^2.8.1", 28 | "typescript": "~4.9.4", 29 | "vitest": "^0.28.5" 30 | }, 31 | "workspaces": [ 32 | "packages/*" 33 | ], 34 | "simple-git-hooks": { 35 | "pre-commit": "npx lint-staged" 36 | }, 37 | "lint-staged": { 38 | "*.{js,ts,json,md}": [ 39 | "eslint --cache --fix" 40 | ] 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /packages/vitest-codemod/src/utils/getTransformDescription.ts: -------------------------------------------------------------------------------- 1 | import type { VitestCodemodTransform } from '@vitest-codemod/types' 2 | 3 | const getWrappedBlocks = (sentence: string, blockLength: number): string[] => { 4 | const words = sentence.split(' ') 5 | const blocks = [] 6 | let currentBlock = '' 7 | 8 | // iterate over the words and add them to blocks 9 | for (const word of words) { 10 | // if the current block plus the next word is longer than the block length, 11 | // add the current block to the list of blocks and start a new block 12 | if (currentBlock.length + word.length > blockLength) { 13 | blocks.push(currentBlock) 14 | currentBlock = '' 15 | } 16 | 17 | // add the word to the current block 18 | currentBlock += `${word} ` 19 | } 20 | 21 | // add the final block to the list of blocks 22 | blocks.push(currentBlock) 23 | 24 | return blocks 25 | } 26 | 27 | export const getTransformDescription = (transform: VitestCodemodTransform): string[] => { 28 | const descriptionArr: string[] = [] 29 | 30 | const columnLength = 15 31 | const borderLength = 2 32 | 33 | descriptionArr[0] 34 | = ' '.repeat(columnLength - borderLength - transform.name.length) 35 | + transform.name 36 | + ' '.repeat(borderLength) 37 | 38 | const wrappedBlocks = getWrappedBlocks(transform.description, 80) 39 | 40 | descriptionArr[0] += wrappedBlocks[0] 41 | for (let i = 1; i < wrappedBlocks.length; i++) 42 | descriptionArr.push(' '.repeat(columnLength) + wrappedBlocks[i]) 43 | 44 | return descriptionArr 45 | } 46 | -------------------------------------------------------------------------------- /packages/jest/src/transformer.ts: -------------------------------------------------------------------------------- 1 | import type { API, FileInfo } from 'jscodeshift' 2 | import { 3 | addFactoryFunctionToMock, 4 | getApisFromCallExpression, 5 | getApisFromMemberExpression, 6 | replaceJestObjectWithVi, 7 | replaceTestApiFailing, 8 | replaceTestApiFit, 9 | updateDefaultExportMocks, 10 | } from './apis' 11 | import { prependImport } from './modules' 12 | import { getSnapshotWithoutPrototype } from './snapshots' 13 | 14 | const transformer = async (file: FileInfo, api: API) => { 15 | if (file.path.endsWith('.snap')) 16 | return getSnapshotWithoutPrototype(file.source) 17 | 18 | const j = api.jscodeshift 19 | const source = j(file.source) 20 | 21 | const apisFromCallExpression = getApisFromCallExpression(j, source) 22 | const apisFromMemberExpression = getApisFromMemberExpression(j, source) 23 | const vitestApis = [...new Set([...apisFromCallExpression, ...apisFromMemberExpression])] 24 | 25 | if (vitestApis.length) { 26 | vitestApis.sort() 27 | const importSpecifiers = vitestApis.map(apiName => j.importSpecifier(j.identifier(apiName))) 28 | const importDeclaration = j.importDeclaration(importSpecifiers, j.stringLiteral('vitest')) 29 | prependImport(j, source, importDeclaration) 30 | } 31 | 32 | replaceTestApiFit(j, source) 33 | replaceTestApiFailing(j, source) 34 | 35 | addFactoryFunctionToMock(j, source) 36 | updateDefaultExportMocks(j, source, file.path) 37 | replaceJestObjectWithVi(j, source) 38 | 39 | source.find(j.ImportDeclaration, { source: { value: '@jest/globals' } }).remove() 40 | 41 | return source.toSource() 42 | } 43 | 44 | export default transformer 45 | -------------------------------------------------------------------------------- /packages/jest/src/apis/getApisFromMemberExpression.ts: -------------------------------------------------------------------------------- 1 | import type { Collection, Identifier, JSCodeshift } from 'jscodeshift' 2 | 3 | const testApiProps = ['concurrent', 'each', 'only', 'skip', 'todo', 'failing'] 4 | const jestGlobalApiProps = { 5 | describe: ['each', 'only', 'skip'], 6 | fit: ['each', 'failing'], 7 | it: testApiProps, 8 | test: testApiProps, 9 | } 10 | 11 | const jestToVitestApiMap: Record = { 12 | fit: 'it', 13 | jest: 'vi', 14 | } 15 | 16 | export const getApisFromMemberExpression = (j: JSCodeshift, source: Collection): string[] => { 17 | const apisFromMemberExpression = [] 18 | 19 | for (const [jestApi, jestApiProps] of Object.entries(jestGlobalApiProps)) { 20 | const propNamesList = source.find(j.MemberExpression, { 21 | object: { name: jestApi }, 22 | property: { type: 'Identifier' }, 23 | }).nodes().map(node => (node.property as Identifier).name) 24 | 25 | const propNames = [...new Set(propNamesList)] 26 | for (const propName of propNames) { 27 | if (jestApiProps.includes(propName)) { 28 | apisFromMemberExpression.push(jestToVitestApiMap[jestApi] ?? jestApi) 29 | break 30 | } 31 | } 32 | } 33 | 34 | const jestObjectName = 'jest' 35 | const jestObjectApiCalls = source.find(j.MemberExpression, { 36 | object: { name: jestObjectName }, 37 | property: { type: 'Identifier' }, 38 | }).filter(path => (path.node.property as Identifier).name !== 'disableAutomock') 39 | 40 | if (jestObjectApiCalls.length) 41 | apisFromMemberExpression.push(jestToVitestApiMap[jestObjectName] ?? jestObjectName) 42 | 43 | return apisFromMemberExpression 44 | } 45 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.yml: -------------------------------------------------------------------------------- 1 | name: Feature Request 2 | description: A generic feature request about vitest-codemod. 3 | title: "[Feature]: " 4 | labels: ["enhancement"] 5 | 6 | body: 7 | - type: checkboxes 8 | id: selfservice 9 | attributes: 10 | label: Self-service 11 | description: "Adding features is a great way to give back to open-source projects, and we're more than happy to answer questions and provide context." 12 | options: 13 | - label: "I'd be willing to implement this feature" 14 | 15 | - type: textarea 16 | attributes: 17 | label: Problem 18 | description: | 19 | Is your feature request related to a problem? Please describe. 20 | placeholder: | 21 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 22 | validations: 23 | required: true 24 | 25 | - type: textarea 26 | attributes: 27 | label: Solution 28 | description: Describe the solution you'd like. 29 | placeholder: | 30 | A clear and concise description of what you want to happen. 31 | validations: 32 | required: true 33 | 34 | - type: textarea 35 | attributes: 36 | label: Alternatives 37 | description: Describe alternatives you've considered 38 | placeholder: | 39 | A clear and concise description of any alternative solutions or features you've considered. 40 | validations: 41 | required: true 42 | 43 | - type: textarea 44 | attributes: 45 | label: Additional context 46 | description: | 47 | Add any other context about the problem here. Or a screenshot if applicable. 48 | -------------------------------------------------------------------------------- /packages/vitest-codemod/README.md: -------------------------------------------------------------------------------- 1 | # vitest-codemod 2 | 3 | This repository contains a collection of codemod scripts for use with 4 | [JSCodeshift][jscodeshift] that help migrate to [vitest][vitest]. 5 | 6 | The `vitest-codemod` CLI is a lightweight wrapper over jscodeshift. 7 | It processes `--help`, `--version` and `--transform` options before passing them 8 | downstream. 9 | 10 | ## Prerequisites 11 | 12 | To use vitest-codemod, please install [Node.js][install-nodejs]. 13 | 14 | ## Usage 15 | 16 | - Optionally execute dry-run for the transform, and print transformed files on stdout: 17 | ```console 18 | npx vitest-codemod --dry --print -t jest PATH... 19 | ``` 20 | - Run transform, and make changes to files: 21 | ```console 22 | npx vitest-codemod -t jest PATH... 23 | ``` 24 | - To use the latest version of vitest-codemod, clear your npx cache. You can either 25 | manually delete folder `$(npm get cache)/_npx/*`, or run `clear-npx-cache`. 26 | ```console 27 | npx clear-npx-cache 28 | ``` 29 | 30 | ## Example 31 | 32 | ```console 33 | $ cat example.spec.js 34 | describe("basic", () => { 35 | test("Math.sqrt()", () => { 36 | expect(Math.sqrt(4)).toBe(2); 37 | }) 38 | }); 39 | 40 | $ npx vitest-codemod -t jest example.spec.js 41 | 42 | $ cat example.spec.js 43 | import { describe, expect, test } from "vitest"; 44 | describe("basic", () => { 45 | test("Math.sqrt()", () => { 46 | expect(Math.sqrt(4)).toBe(2); 47 | }) 48 | }); 49 | ``` 50 | 51 | ## License 52 | 53 | This library is licensed under the MIT License. See the LICENSE file. 54 | 55 | [install-nodejs]: https://nodejs.dev/learn/how-to-install-nodejs 56 | [jscodeshift]: https://github.com/facebook/jscodeshift 57 | [vitest]: https://vitest.dev/ -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toHaveProperty.input.js: -------------------------------------------------------------------------------- 1 | // Object containing house features to be tested 2 | const houseForSale = { 3 | bath: true, 4 | bedrooms: 4, 5 | kitchen: { 6 | amenities: ["oven", "stove", "washer"], 7 | area: 20, 8 | wallColor: "white", 9 | "nice.oven": true, 10 | }, 11 | livingroom: { 12 | amenities: [ 13 | { 14 | couch: [ 15 | ["large", { dimensions: [20, 20] }], 16 | ["small", { dimensions: [10, 10] }], 17 | ], 18 | }, 19 | ], 20 | }, 21 | "ceiling.height": "tall", 22 | }; 23 | 24 | test("this house has my desired features", () => { 25 | // Example Referencing 26 | expect(houseForSale).toHaveProperty("bath"); 27 | expect(houseForSale).toHaveProperty("bedrooms", 4); 28 | 29 | expect(houseForSale).not.toHaveProperty("pool"); 30 | 31 | // Deep referencing using dot notation 32 | expect(houseForSale).toHaveProperty("kitchen.area", 20); 33 | expect(houseForSale).toHaveProperty("kitchen.amenities", [ 34 | "oven", 35 | "stove", 36 | "washer", 37 | ]); 38 | 39 | expect(houseForSale).not.toHaveProperty("kitchen.open"); 40 | 41 | // Deep referencing using an array containing the keyPath 42 | expect(houseForSale).toHaveProperty(["kitchen", "area"], 20); 43 | expect(houseForSale).toHaveProperty( 44 | ["kitchen", "amenities"], 45 | ["oven", "stove", "washer"] 46 | ); 47 | expect(houseForSale).toHaveProperty(["kitchen", "amenities", 0], "oven"); 48 | expect(houseForSale).toHaveProperty( 49 | "livingroom.amenities[0].couch[0][1].dimensions[0]", 50 | 20 51 | ); 52 | expect(houseForSale).toHaveProperty(["kitchen", "nice.oven"]); 53 | expect(houseForSale).not.toHaveProperty(["kitchen", "open"]); 54 | 55 | // Referencing keys with dot in the key itself 56 | expect(houseForSale).toHaveProperty(["ceiling.height"], "tall"); 57 | }); 58 | -------------------------------------------------------------------------------- /packages/jest/src/__fixtures__/expect/toHaveProperty.output.js: -------------------------------------------------------------------------------- 1 | import { expect, test } from "vitest"; 2 | // Object containing house features to be tested 3 | const houseForSale = { 4 | bath: true, 5 | bedrooms: 4, 6 | kitchen: { 7 | amenities: ["oven", "stove", "washer"], 8 | area: 20, 9 | wallColor: "white", 10 | "nice.oven": true, 11 | }, 12 | livingroom: { 13 | amenities: [ 14 | { 15 | couch: [ 16 | ["large", { dimensions: [20, 20] }], 17 | ["small", { dimensions: [10, 10] }], 18 | ], 19 | }, 20 | ], 21 | }, 22 | "ceiling.height": "tall", 23 | }; 24 | 25 | test("this house has my desired features", () => { 26 | // Example Referencing 27 | expect(houseForSale).toHaveProperty("bath"); 28 | expect(houseForSale).toHaveProperty("bedrooms", 4); 29 | 30 | expect(houseForSale).not.toHaveProperty("pool"); 31 | 32 | // Deep referencing using dot notation 33 | expect(houseForSale).toHaveProperty("kitchen.area", 20); 34 | expect(houseForSale).toHaveProperty("kitchen.amenities", [ 35 | "oven", 36 | "stove", 37 | "washer", 38 | ]); 39 | 40 | expect(houseForSale).not.toHaveProperty("kitchen.open"); 41 | 42 | // Deep referencing using an array containing the keyPath 43 | expect(houseForSale).toHaveProperty(["kitchen", "area"], 20); 44 | expect(houseForSale).toHaveProperty( 45 | ["kitchen", "amenities"], 46 | ["oven", "stove", "washer"] 47 | ); 48 | expect(houseForSale).toHaveProperty(["kitchen", "amenities", 0], "oven"); 49 | expect(houseForSale).toHaveProperty( 50 | "livingroom.amenities[0].couch[0][1].dimensions[0]", 51 | 20 52 | ); 53 | expect(houseForSale).toHaveProperty(["kitchen", "nice.oven"]); 54 | expect(houseForSale).not.toHaveProperty(["kitchen", "open"]); 55 | 56 | // Referencing keys with dot in the key itself 57 | expect(houseForSale).toHaveProperty(["ceiling.height"], "tall"); 58 | }); 59 | -------------------------------------------------------------------------------- /packages/vitest-codemod/src/cli.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | // Most of the code from here is from bin/jscodeshift.js 4 | // It's kept that way so that users can reuse jscodeshift options. 5 | 6 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment 7 | // @ts-nocheck 8 | import path from 'path' 9 | import Runner from 'jscodeshift/dist/Runner' 10 | 11 | import { 12 | getHelpParagraph, 13 | getJsCodeshiftParser, 14 | getTransforms, 15 | getUpdatedTransformFile, 16 | } from './utils' 17 | 18 | const args = process.argv 19 | const transforms = getTransforms() 20 | 21 | if (args[2] === '--help' || args[2] === '-h') 22 | process.stdout.write(getHelpParagraph(transforms)) 23 | 24 | const parser = getJsCodeshiftParser() 25 | 26 | let options, positionalArguments 27 | try { 28 | ({ options, positionalArguments } = parser.parse()) 29 | if (positionalArguments.length === 0 && !options.stdin) { 30 | process.stderr.write( 31 | 'Error: You have to provide at least one file/directory to transform.' 32 | + `\n\n---\n\n${ 33 | parser.getHelpText()}`, 34 | ) 35 | process.exit(1) 36 | } 37 | } 38 | catch (e) { 39 | const exitCode = e.exitCode === undefined ? 1 : e.exitCode; 40 | (exitCode ? process.stderr : process.stdout).write(e.message) 41 | process.exit(exitCode) 42 | } 43 | 44 | const { transform } = options 45 | if (transforms.map(({ name }) => name).includes(transform)) 46 | options.transform = getUpdatedTransformFile(transform) 47 | 48 | function run(paths, options) { 49 | Runner.run( 50 | /^https?/.test(options.transform) ? options.transform : path.resolve(options.transform), 51 | paths, 52 | options, 53 | ) 54 | } 55 | 56 | if (options.stdin) { 57 | let buffer = '' 58 | process.stdin.on('data', data => (buffer += data)) 59 | process.stdin.on('end', () => run(buffer.split('\n'), options)) 60 | } 61 | else { 62 | run(positionalArguments, options) 63 | } 64 | -------------------------------------------------------------------------------- /packages/jest/src/apis/replaceJestObjectWithVi.ts: -------------------------------------------------------------------------------- 1 | import type { Collection, JSCodeshift } from 'jscodeshift' 2 | 3 | const apiNamesRecord: Record = { 4 | createMockFromModule: 'importMock', 5 | deepUnmock: 'unmock', 6 | genMockFromModule: 'importMock', 7 | requireActual: 'importActual', 8 | requireMock: 'importMock', 9 | setMock: 'mock', 10 | } 11 | const apiNamesToMakeAsync = ['genMockFromModule', 'createMockFromModule', 'requireActual', 'requireMock'] 12 | 13 | export const replaceJestObjectWithVi = (j: JSCodeshift, source: Collection): void => { 14 | // Replace `jest` with `vi` 15 | source.find(j.MemberExpression, { 16 | object: { type: 'Identifier', name: 'jest' }, 17 | }).forEach((path) => { 18 | if (path.node.property.type === 'Identifier') { 19 | const propertyName = path.node.property.name 20 | 21 | if (propertyName === 'enableAutomock') { 22 | throw new Error( 23 | `The automocking API "${propertyName}" is not supported in vitest.\n` 24 | + 'See https://vitest.dev/guide/migration.html', 25 | ) 26 | } 27 | 28 | if (propertyName === 'disableAutomock') { 29 | j(path.parentPath).remove() 30 | return 31 | } 32 | 33 | if (apiNamesRecord[propertyName]) 34 | path.node.property.name = apiNamesRecord[propertyName] 35 | 36 | if (apiNamesToMakeAsync.includes(propertyName)) { 37 | // Add await to the call expression 38 | j(path.parentPath).replaceWith(path => 39 | j.awaitExpression(path.value), 40 | ) 41 | 42 | // Add async to the function 43 | let parentPath = path.parentPath 44 | while (!['FunctionExpression', 'ArrowFunctionExpression'].includes(parentPath.value.type)) 45 | parentPath = parentPath.parentPath 46 | parentPath.value.async = true 47 | } 48 | } 49 | 50 | path.node.object = j.identifier('vi') 51 | return path 52 | }) 53 | } 54 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request_transform.yml: -------------------------------------------------------------------------------- 1 | name: Feature Request (transform) 2 | description: Feature request specific to a particular transform (like `jest`) 3 | title: "[Feature]: " 4 | labels: ["enhancement"] 5 | 6 | body: 7 | - type: checkboxes 8 | id: selfservice 9 | attributes: 10 | label: Self-service 11 | description: "Adding features is a great way to give back to open-source projects, and we're more than happy to answer questions and provide context." 12 | options: 13 | - label: "I'd be willing to implement this feature" 14 | 15 | - type: dropdown 16 | id: template-name 17 | attributes: 18 | label: Template name 19 | description: Which template are you adding this feature request for? 20 | options: 21 | - jest 22 | - N/A (described above) 23 | validations: 24 | required: true 25 | 26 | - type: textarea 27 | attributes: 28 | label: Input code 29 | description: | 30 | Provide a minimal code for which you need transformation to be supported. 31 | placeholder: | 32 | ```ts 33 | describe('Math', () => { 34 | test('.sqrt()', () => { 35 | expect(Math.sqrt(4)).toBe(2) 36 | expect(Math.sqrt(144)).toBe(12) 37 | expect(Math.sqrt(2)).toBe(Math.SQRT2) 38 | }) 39 | }); 40 | ``` 41 | validations: 42 | required: true 43 | 44 | - type: textarea 45 | attributes: 46 | label: Expected Output 47 | description: The expected transformed output for the provided code snippet. 48 | placeholder: | 49 | ```ts 50 | import { expect, describe, test } from 'vitest' 51 | 52 | describe('Math', () => { 53 | test('.sqrt()', () => { 54 | expect(Math.sqrt(4)).toBe(2) 55 | expect(Math.sqrt(144)).toBe(12) 56 | expect(Math.sqrt(2)).toBe(Math.SQRT2) 57 | }) 58 | }); 59 | ``` 60 | 61 | - type: textarea 62 | attributes: 63 | label: Additional context 64 | description: | 65 | Add any other context about the problem here. Or a screenshot if applicable. 66 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | 106 | # yarn berry with no zero installs 107 | .yarn/* 108 | !.yarn/releases 109 | !.yarn/plugins 110 | !.yarn/sdks 111 | !.yarn/versions 112 | .pnp.* -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.yml: -------------------------------------------------------------------------------- 1 | name: Bug Report 2 | description: A generic bug report about vitest-codemod. 3 | title: "[Bug?]: " 4 | labels: ["bug", "triage"] 5 | 6 | body: 7 | - type: checkboxes 8 | id: selfservice 9 | attributes: 10 | label: Self-service 11 | description: "Fixing bugs is always a great way to give back to open-source projects, and we're more than happy to answer questions and provide context." 12 | options: 13 | - label: "I'd be willing to implement a fix" 14 | 15 | - type: textarea 16 | attributes: 17 | label: Describe the bug 18 | description: | 19 | A clear and concise description of what the bug is. A bug is **unintended**. A feature not being implemented is **not a bug**. 20 | placeholder: | 21 | eg: vitest-codemod crashes with a specific error. 22 | validations: 23 | required: true 24 | 25 | - type: textarea 26 | attributes: 27 | label: Steps to reproduce 28 | description: The _minimal_ steps to reproduce your issue. 29 | placeholder: | 30 | Please share code or minimal repo, and steps to reproduce the behavior. 31 | validations: 32 | required: true 33 | 34 | - type: textarea 35 | attributes: 36 | label: Observed behavior 37 | description: A clear and concise description of what happens. 38 | placeholder: | 39 | eg: The vitest-codemod script returned error with the following stack trace: 40 | validations: 41 | required: true 42 | 43 | - type: textarea 44 | attributes: 45 | label: Expected behavior 46 | description: A clear and concise description of what you were expecting to happen. 47 | placeholder: | 48 | eg: The vitest-codemod script should have run successfully. 49 | validations: 50 | required: true 51 | 52 | - type: textarea 53 | attributes: 54 | label: Environment 55 | render: shell 56 | description: | 57 | Run `vitest-codemod --version` and paste the result into the form 58 | placeholder: | 59 | vitest-codemod: 0.1.0 60 | - jscodeshift: 0.14.0 61 | - recast: 0.21.5 62 | validations: 63 | required: true 64 | 65 | - type: textarea 66 | attributes: 67 | label: Additional context 68 | description: | 69 | Add any other context about the problem here. Or a screenshot if applicable. 70 | -------------------------------------------------------------------------------- /packages/jest/src/apis/updateDefaultExportMocks.ts: -------------------------------------------------------------------------------- 1 | import { dirname, join, resolve } from 'path' 2 | import type { 3 | ArrowFunctionExpression, Collection, FunctionExpression, 4 | Identifier, JSCodeshift, MemberExpression, ObjectProperty, 5 | } from 'jscodeshift' 6 | 7 | export const updateDefaultExportMocks = (j: JSCodeshift, source: Collection, filePath: string) => { 8 | source.find(j.CallExpression, { 9 | callee: { 10 | type: 'MemberExpression', 11 | object: { type: 'Identifier', name: 'jest' }, 12 | property: { type: 'Identifier' }, 13 | }, 14 | }).filter(path => ['mock', 'setMock'].includes( 15 | ((path.value.callee as MemberExpression).property as Identifier).name, 16 | )).forEach((path) => { 17 | const { arguments: args } = path.value 18 | 19 | if (args.length < 2) 20 | return 21 | 22 | const [moduleName, mock] = args 23 | 24 | if (!['ArrowFunctionExpression', 'FunctionExpression'].includes(mock.type)) 25 | return 26 | 27 | if (moduleName.type !== 'Literal' && moduleName.type !== 'StringLiteral') 28 | return 29 | 30 | // @ts-expect-error - moduleName is a Literal/StringLiteral 31 | const modulePath = resolve(join(dirname(filePath), moduleName.value)) 32 | 33 | // eslint-disable-next-line @typescript-eslint/no-var-requires 34 | const module = require(modulePath) 35 | 36 | if (typeof module === 'object') 37 | return 38 | 39 | if (mock.type === 'ArrowFunctionExpression') { 40 | const mockBody = mock.body 41 | if ( 42 | mockBody.type === 'ObjectExpression' 43 | && mockBody.properties.map(p => ((p as ObjectProperty).key as Identifier).name).includes('default') 44 | ) 45 | return 46 | 47 | if (mockBody.type !== 'BlockStatement') { 48 | mock.body = j.objectExpression([ 49 | j.property('init', j.identifier('default'), mockBody), 50 | ]) 51 | return 52 | } 53 | } 54 | 55 | const mockBody = (mock as FunctionExpression | ArrowFunctionExpression).body 56 | if (mockBody.type === 'BlockStatement') { 57 | const returnStatement = mockBody.body[mockBody.body.length - 1] 58 | if (returnStatement.type === 'ReturnStatement') { 59 | const returnArgument = returnStatement.argument 60 | if (returnArgument) { 61 | if ( 62 | returnArgument.type === 'ObjectExpression' 63 | && returnArgument.properties.map(p => ((p as ObjectProperty).key as Identifier).name).includes('default')) 64 | return 65 | returnStatement.argument = j.objectExpression([ 66 | j.property('init', j.identifier('default'), returnArgument), 67 | ]) 68 | } 69 | } 70 | } 71 | }) 72 | } 73 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report_transform.yml: -------------------------------------------------------------------------------- 1 | name: Bug Report (transform) 2 | description: Bug report specific to a particular transform (like `jest`) 3 | title: "[Bug?]: " 4 | labels: ["bug", "triage"] 5 | 6 | body: 7 | - type: checkboxes 8 | id: selfservice 9 | attributes: 10 | label: Self-service 11 | description: "Fixing bugs is always a great way to give back to open-source projects, and we're more than happy to answer questions and provide context." 12 | options: 13 | - label: "I'd be willing to implement a fix" 14 | 15 | - type: dropdown 16 | id: template-name 17 | attributes: 18 | label: Template name 19 | description: Which template are you seeing this error for? 20 | options: 21 | - jest 22 | - N/A (described above) 23 | validations: 24 | required: true 25 | 26 | - type: textarea 27 | attributes: 28 | label: Describe the bug 29 | description: | 30 | A clear and concise description of what the bug is. A bug is **unintended**. A feature not being implemented is **not a bug**. 31 | placeholder: | 32 | eg: Running transform "transformName" on the provided input causes it to crash. 33 | validations: 34 | required: true 35 | 36 | - type: textarea 37 | attributes: 38 | label: Input code 39 | description: The _minimal_ input file to reproduce your issue. 40 | placeholder: | 41 | ```ts 42 | describe('Math', () => { 43 | test('.sqrt()', () => { 44 | expect(Math.sqrt(4)).toBe(2) 45 | expect(Math.sqrt(144)).toBe(12) 46 | expect(Math.sqrt(2)).toBe(Math.SQRT2) 47 | }) 48 | }); 49 | ``` 50 | validations: 51 | required: true 52 | 53 | - type: textarea 54 | attributes: 55 | label: Observed failure 56 | description: A concise description of what error you're experiencing. 57 | placeholder: | 58 | eg: The vitest-codemod script returned the following error: 59 | validations: 60 | required: true 61 | 62 | - type: textarea 63 | attributes: 64 | label: Expected output 65 | description: The output file you expected to see. 66 | placeholder: | 67 | ```ts 68 | import { expect, describe, test } from 'vitest' 69 | 70 | describe('Math', () => { 71 | test('.sqrt()', () => { 72 | expect(Math.sqrt(4)).toBe(2) 73 | expect(Math.sqrt(144)).toBe(12) 74 | expect(Math.sqrt(2)).toBe(Math.SQRT2) 75 | }) 76 | }); 77 | ``` 78 | validations: 79 | required: true 80 | 81 | - type: textarea 82 | attributes: 83 | label: Environment 84 | render: shell 85 | description: | 86 | Run `vitest-codemod --version` and paste the result into the form 87 | placeholder: | 88 | vitest-codemod: 0.1.0 89 | - jscodeshift: 0.14.0 90 | - recast: 0.21.5 91 | validations: 92 | required: true 93 | 94 | - type: textarea 95 | attributes: 96 | label: Additional context 97 | description: | 98 | Add any other context about the problem here. Or a screenshot if applicable. 99 | -------------------------------------------------------------------------------- /packages/jest/src/transformer.spec.ts: -------------------------------------------------------------------------------- 1 | import { readdirSync } from 'fs' 2 | import { readFile } from 'fs/promises' 3 | import { join } from 'path' 4 | import jscodeshift from 'jscodeshift' 5 | import { describe, expect, it } from 'vitest' 6 | 7 | import transform from './transformer' 8 | 9 | describe('transformer', () => { 10 | const inputFileRegex = /(.*).input.m?[jt]sx?$/ 11 | const errorFileRegex = /(.*).error.m?[jt]sx?$/ 12 | 13 | const fixtureDir = join(__dirname, '__fixtures__') 14 | const fixtureSubDirs = readdirSync(fixtureDir, { withFileTypes: true }) 15 | .filter(dirent => dirent.isDirectory()) 16 | .map(dirent => dirent.name) 17 | 18 | const getTestFileMetadata = (dirPath: string, fileRegex: RegExp) => 19 | readdirSync(dirPath) 20 | .filter(fileName => fileRegex.test(fileName)) 21 | .map( 22 | fileName => 23 | [ 24 | (fileName.match(fileRegex) as RegExpMatchArray)[1], 25 | fileName.split('.').pop() as string, 26 | ] as const, 27 | ) 28 | 29 | const getTestFileInput = async (dirPath: string, fileName: string) => { 30 | const inputPath = join(dirPath, fileName) 31 | const inputCode = await readFile(inputPath, 'utf8') 32 | return { path: inputPath, source: inputCode } 33 | } 34 | 35 | const getTestFileOutputCode = async (dirPath: string, fileName: string) => 36 | readFile(join(dirPath, fileName), 'utf8') 37 | 38 | describe.each(fixtureSubDirs)('%s', (subDir) => { 39 | const subDirPath = join(fixtureDir, subDir) 40 | 41 | it.concurrent.each(getTestFileMetadata(subDirPath, inputFileRegex))( 42 | 'transforms: %s.%s', 43 | async (filePrefix, fileExtension) => { 44 | const inputFileName = [filePrefix, 'input', fileExtension].join('.') 45 | const outputFileName = [filePrefix, 'output', fileExtension].join('.') 46 | 47 | const input = await getTestFileInput(subDirPath, inputFileName) 48 | const outputCode = await getTestFileOutputCode(subDirPath, outputFileName) 49 | 50 | const output = await transform(input, { 51 | j: jscodeshift, 52 | jscodeshift, 53 | stats: () => {}, 54 | report: () => {}, 55 | }) 56 | 57 | expect(output.trim()).toEqual(outputCode.trim()) 58 | }, 59 | 60000, 60 | ) 61 | 62 | it.concurrent.each(getTestFileMetadata(subDirPath, errorFileRegex))( 63 | 'throws: %s.%s', 64 | async (filePrefix, fileExtension) => { 65 | const inputFileName = [filePrefix, 'error', fileExtension].join('.') 66 | const input = await getTestFileInput(subDirPath, inputFileName) 67 | 68 | await expect( 69 | transform(input, { 70 | j: jscodeshift, 71 | jscodeshift, 72 | stats: () => {}, 73 | report: () => {}, 74 | }), 75 | ).rejects.toThrowError() 76 | }, 77 | ) 78 | }) 79 | 80 | describe('.snap', () => { 81 | it.each([ 82 | [ 83 | 'Empty array', 84 | 'exports[`snapshot 1`] = `Array []`;', 85 | 'exports[`snapshot 1`] = `[]`;', 86 | ], 87 | [ 88 | 'Empty object', 89 | 'exports[`snapshot 1`] = `Object {}`;', 90 | 'exports[`snapshot 1`] = `{}`;', 91 | ], 92 | [ 93 | 'Nested array+object', 94 | `exports[\`snapshot 1\`] = \` 95 | Array [ 96 | Object { 97 | "foo": "bar", 98 | }, 99 | ] 100 | \`;`, 101 | `exports[\`snapshot 1\`] = \` 102 | [ 103 | { 104 | "foo": "bar", 105 | }, 106 | ] 107 | \`;`, 108 | ], 109 | ])('%s', async (testName, inputCode, outputCode) => { 110 | const input = { 111 | path: 'test.js.snap', 112 | source: inputCode, 113 | } 114 | 115 | const output = await transform(input, { 116 | j: jscodeshift, 117 | jscodeshift, 118 | stats: () => {}, 119 | report: () => {}, 120 | }) 121 | 122 | expect(output.trim()).toEqual(outputCode) 123 | }) 124 | }) 125 | }) 126 | -------------------------------------------------------------------------------- /packages/vitest-codemod/src/utils/getJsCodeshiftParser.ts: -------------------------------------------------------------------------------- 1 | // Most of the code from here is from bin/jscodeshift.js 2 | // It's kept that way so that users can reuse jscodeshift options. 3 | 4 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment 5 | // @ts-nocheck 6 | 7 | import { readFileSync } from 'fs' 8 | import { dirname, join } from 'path' 9 | import argsParser from 'jscodeshift/dist/argsParser' 10 | 11 | // @ts-expect-error: package.json will be imported from dist folders 12 | import { version } from '../../package.json' 13 | 14 | const requirePackage = (name: string) => { 15 | const entry = require.resolve(name) 16 | let dir = dirname(entry) 17 | while (dir !== '/') { 18 | try { 19 | // eslint-disable-next-line @typescript-eslint/no-var-requires 20 | const pkg = require(join(dir, 'package.json')) 21 | return pkg.name === name ? pkg : {} 22 | } 23 | catch (error) {} 24 | dir = dirname(dir) 25 | } 26 | return {} 27 | } 28 | 29 | export const getJsCodeshiftParser = () => 30 | argsParser.options({ 31 | transform: { 32 | display_index: 15, 33 | abbr: 't', 34 | default: './transform.js', 35 | help: 'path to the transform file. Can be either a local path or url', 36 | metavar: 'FILE', 37 | required: true, 38 | }, 39 | cpus: { 40 | display_index: 1, 41 | abbr: 'c', 42 | help: 'start at most N child processes to process source files', 43 | defaultHelp: 'max(all - 1, 1)', 44 | metavar: 'N', 45 | process: Number, 46 | }, 47 | verbose: { 48 | display_index: 16, 49 | abbr: 'v', 50 | choices: [0, 1, 2], 51 | default: 0, 52 | help: 'show more information about the transform process', 53 | metavar: 'N', 54 | process: Number, 55 | }, 56 | dry: { 57 | display_index: 2, 58 | abbr: 'd', 59 | flag: true, 60 | default: false, 61 | help: 'dry run (no changes are made to files)', 62 | }, 63 | print: { 64 | display_index: 11, 65 | abbr: 'p', 66 | flag: true, 67 | default: false, 68 | help: 'print transformed files to stdout, useful for development', 69 | }, 70 | babel: { 71 | display_index: 0, 72 | flag: true, 73 | default: true, 74 | help: 'apply babeljs to the transform file', 75 | }, 76 | extensions: { 77 | display_index: 3, 78 | default: 'js', 79 | help: 'transform files with these file extensions (comma separated list)', 80 | metavar: 'EXT', 81 | }, 82 | ignorePattern: { 83 | display_index: 7, 84 | full: 'ignore-pattern', 85 | list: true, 86 | help: 'ignore files that match a provided glob expression', 87 | metavar: 'GLOB', 88 | }, 89 | ignoreConfig: { 90 | display_index: 6, 91 | full: 'ignore-config', 92 | list: true, 93 | help: 'ignore files if they match patterns sourced from a configuration file (e.g. a .gitignore)', 94 | metavar: 'FILE', 95 | }, 96 | gitignore: { 97 | display_index: 8, 98 | flag: true, 99 | default: false, 100 | help: 'adds entries the current directory\'s .gitignore file', 101 | }, 102 | runInBand: { 103 | display_index: 12, 104 | flag: true, 105 | default: false, 106 | full: 'run-in-band', 107 | help: 'run serially in the current process', 108 | }, 109 | silent: { 110 | display_index: 13, 111 | abbr: 's', 112 | flag: true, 113 | default: false, 114 | help: 'do not write to stdout or stderr', 115 | }, 116 | parser: { 117 | display_index: 9, 118 | choices: ['babel', 'babylon', 'flow', 'ts', 'tsx'], 119 | default: 'babylon', 120 | help: 'the parser to use for parsing the source files', 121 | }, 122 | parserConfig: { 123 | display_index: 10, 124 | full: 'parser-config', 125 | help: 'path to a JSON file containing a custom parser configuration for flow or babylon', 126 | metavar: 'FILE', 127 | process: (file: string) => JSON.parse(readFileSync(file).toString()), 128 | }, 129 | failOnError: { 130 | display_index: 4, 131 | flag: true, 132 | help: 'Return a non-zero code when there are errors', 133 | full: 'fail-on-error', 134 | default: false, 135 | }, 136 | version: { 137 | display_index: 17, 138 | help: 'print version and exit', 139 | callback() { 140 | return [ 141 | `vitest-codemod: ${version}`, 142 | `- jscodeshift: ${requirePackage('jscodeshift').version}`, 143 | `- recast: ${requirePackage('recast').version}\n`, 144 | ].join('\n') 145 | }, 146 | }, 147 | stdin: { 148 | display_index: 14, 149 | help: 'read file/directory list from stdin', 150 | flag: true, 151 | default: false, 152 | }, 153 | }) 154 | -------------------------------------------------------------------------------- /packages/vitest-codemod/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # vitest-codemod 2 | 3 | ## 0.1.3 4 | 5 | ### Patch Changes 6 | 7 | - [#100](https://github.com/trivikr/vitest-codemod/pull/100) [`76a430f`](https://github.com/trivikr/vitest-codemod/commit/76a430f2d001e161e8b4b8f4109e6c13eaeb96ab) Thanks [@trivikr](https://github.com/trivikr)! - Support Jest APIs which change to async in Vitest 8 | 9 | - Updated dependencies [[`76a430f`](https://github.com/trivikr/vitest-codemod/commit/76a430f2d001e161e8b4b8f4109e6c13eaeb96ab), [`8a1a1c6`](https://github.com/trivikr/vitest-codemod/commit/8a1a1c6dde6459ffa531363d5903b84af17ec253), [`dd88dc7`](https://github.com/trivikr/vitest-codemod/commit/dd88dc78f40b5854429cc439569a8f69220bd8ae), [`8be7c13`](https://github.com/trivikr/vitest-codemod/commit/8be7c130950e821abed73810acc2c216674bb80d)]: 10 | - @vitest-codemod/jest@0.4.0 11 | 12 | ## 0.1.2 13 | 14 | ### Patch Changes 15 | 16 | - [#80](https://github.com/trivikr/vitest-codemod/pull/80) [`13fb202`](https://github.com/trivikr/vitest-codemod/commit/13fb2021003c31d31ae104bab5b5f4c0da5762ae) Thanks [@trivikr](https://github.com/trivikr)! - Support transformation of jest API to vi 17 | 18 | - Updated dependencies [[`13fb202`](https://github.com/trivikr/vitest-codemod/commit/13fb2021003c31d31ae104bab5b5f4c0da5762ae)]: 19 | - @vitest-codemod/jest@0.3.0 20 | 21 | ## 0.1.1 22 | 23 | ### Patch Changes 24 | 25 | - [#72](https://github.com/trivikr/vitest-codemod/pull/72) [`61d4e88`](https://github.com/trivikr/vitest-codemod/commit/61d4e88847b69ea91085da635ea198b35a9eeed1) Thanks [@trivikr](https://github.com/trivikr)! - Import describe when member expression is used in jest 26 | 27 | - Updated dependencies [[`61d4e88`](https://github.com/trivikr/vitest-codemod/commit/61d4e88847b69ea91085da635ea198b35a9eeed1)]: 28 | - @vitest-codemod/jest@0.2.0 29 | 30 | ## 0.1.0 31 | 32 | ### Minor Changes 33 | 34 | - [#46](https://github.com/trivikr/vitest-codemod/pull/46) [`fc6bbca`](https://github.com/trivikr/vitest-codemod/commit/fc6bbca36632890c09e4f0d4167d152153a1366d) Thanks [@trivikr](https://github.com/trivikr)! - Bump to 0.1.0 as basic transform to support jest has landed 35 | 36 | ### Patch Changes 37 | 38 | - Updated dependencies [[`fc6bbca`](https://github.com/trivikr/vitest-codemod/commit/fc6bbca36632890c09e4f0d4167d152153a1366d)]: 39 | - @vitest-codemod/jest@0.1.0 40 | 41 | ## 0.0.9 42 | 43 | ### Patch Changes 44 | 45 | - [#42](https://github.com/trivikr/vitest-codemod/pull/42) [`9a534e3`](https://github.com/trivikr/vitest-codemod/commit/9a534e3bfc0491886cb752b46e769ea9970af272) Thanks [@trivikr](https://github.com/trivikr)! - Add vitest imports for jest globals 46 | 47 | - Updated dependencies [[`9a534e3`](https://github.com/trivikr/vitest-codemod/commit/9a534e3bfc0491886cb752b46e769ea9970af272)]: 48 | - @vitest-codemod/jest@0.0.5 49 | 50 | ## 0.0.8 51 | 52 | ### Patch Changes 53 | 54 | - [#34](https://github.com/trivikr/vitest-codemod/pull/34) [`8e60373`](https://github.com/trivikr/vitest-codemod/commit/8e60373e71a0530b86f73e18a171d249fea76ed7) Thanks [@trivikr](https://github.com/trivikr)! - Format code with @antfu/eslint-config rules 55 | 56 | - Updated dependencies [[`8e60373`](https://github.com/trivikr/vitest-codemod/commit/8e60373e71a0530b86f73e18a171d249fea76ed7)]: 57 | - @vitest-codemod/jest@0.0.4 58 | 59 | ## 0.0.7 60 | 61 | ### Patch Changes 62 | 63 | - [#30](https://github.com/trivikr/vitest-codemod/pull/30) [`0429895`](https://github.com/trivikr/vitest-codemod/commit/0429895d54d044e1e8f085fb9c5150d635c1f7f2) Thanks [@trivikr](https://github.com/trivikr)! - Default parser to babylon 64 | 65 | - Updated dependencies [[`0429895`](https://github.com/trivikr/vitest-codemod/commit/0429895d54d044e1e8f085fb9c5150d635c1f7f2)]: 66 | - @vitest-codemod/jest@0.0.3 67 | 68 | ## 0.0.6 69 | 70 | ### Patch Changes 71 | 72 | - [#28](https://github.com/trivikr/vitest-codemod/pull/28) [`c6b49e8`](https://github.com/trivikr/vitest-codemod/commit/c6b49e81e02f59438f1d6b2eb8334f6895d2ad4d) Thanks [@trivikr](https://github.com/trivikr)! - Return updated transform file path 73 | 74 | ## 0.0.5 75 | 76 | ### Patch Changes 77 | 78 | - [#26](https://github.com/trivikr/vitest-codemod/pull/26) [`bce0da7`](https://github.com/trivikr/vitest-codemod/commit/bce0da7221212bd13312b065a192d14a29ec40c4) Thanks [@trivikr](https://github.com/trivikr)! - Remove "workspace:" protocol for internal dependencies 79 | 80 | - Updated dependencies [[`bce0da7`](https://github.com/trivikr/vitest-codemod/commit/bce0da7221212bd13312b065a192d14a29ec40c4)]: 81 | - @vitest-codemod/jest@0.0.2 82 | 83 | ## 0.0.4 84 | 85 | ### Patch Changes 86 | 87 | - [#24](https://github.com/trivikr/vitest-codemod/pull/24) [`a69ada5`](https://github.com/trivikr/vitest-codemod/commit/a69ada59e53df7b40e5d036762604b69a7518fe6) Thanks [@trivikr](https://github.com/trivikr)! - Display name and description from @vitest-codemod/jest 88 | 89 | - Updated dependencies [[`fbf7743`](https://github.com/trivikr/vitest-codemod/commit/fbf7743d28b070c8b570d80457cfaf68ebbae432)]: 90 | - @vitest-codemod/jest@0.0.1 91 | 92 | ## 0.0.3 93 | 94 | ### Patch Changes 95 | 96 | - [#10](https://github.com/trivikr/vitest-codemod/pull/10) [`36886bf`](https://github.com/trivikr/vitest-codemod/commit/36886bf3537d05fd0af47a1c5cd4e75e343588e6) Thanks [@trivikr](https://github.com/trivikr)! - Use types from @vitest-codemod/types 97 | 98 | ## 0.0.2 99 | 100 | ### Patch Changes 101 | 102 | - [#5](https://github.com/trivikr/vitest-codemod/pull/5) [`ca662a3`](https://github.com/trivikr/vitest-codemod/commit/ca662a3) Thanks [@trivikr](https://github.com/trivikr)! - Update vitest-codemod monorepo to use yarn workspaces 103 | 104 | ## 0.0.1 105 | 106 | ### Patch Changes 107 | 108 | - [#1](https://github.com/trivikr/vitest-codemod/pull/1) [`153b894`](https://github.com/trivikr/vitest-codemod/commit/153b894) Thanks [@trivikr](https://github.com/trivikr)! - Add initial code 109 | -------------------------------------------------------------------------------- /packages/jest/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # @vitest-codemod/jest 2 | 3 | ## 0.4.5 4 | 5 | ### Patch Changes 6 | 7 | - [#124](https://github.com/trivikr/vitest-codemod/pull/124) [`8a5b795`](https://github.com/trivikr/vitest-codemod/commit/8a5b795c3b3f746dcf7e822b06ac59a4d9087d5a) Thanks [@trivikr](https://github.com/trivikr)! - Add transformation for jest.deepUnmock 8 | 9 | ## 0.4.4 10 | 11 | ### Patch Changes 12 | 13 | - [#122](https://github.com/trivikr/vitest-codemod/pull/122) [`a4dc312`](https://github.com/trivikr/vitest-codemod/commit/a4dc3123c689b5c0a3a4f675b3f834b57da73a75) Thanks [@trivikr](https://github.com/trivikr)! - Support transformation for jest.setMock 14 | 15 | ## 0.4.3 16 | 17 | ### Patch Changes 18 | 19 | - [#118](https://github.com/trivikr/vitest-codemod/pull/118) [`3101933`](https://github.com/trivikr/vitest-codemod/commit/3101933710c43f14029dd5878bb453c8057c639b) Thanks [@trivikr](https://github.com/trivikr)! - Add transformation for jest.genMockFromModule 20 | 21 | ## 0.4.2 22 | 23 | ### Patch Changes 24 | 25 | - [#116](https://github.com/trivikr/vitest-codemod/pull/116) [`b75bfe1`](https://github.com/trivikr/vitest-codemod/commit/b75bfe19722e4b4f2d8ba683caac1e380443daa8) Thanks [@trivikr](https://github.com/trivikr)! - Support transformation of default exports 26 | 27 | ## 0.4.1 28 | 29 | ### Patch Changes 30 | 31 | - [#115](https://github.com/trivikr/vitest-codemod/pull/115) [`66c860c`](https://github.com/trivikr/vitest-codemod/commit/66c860cd02582f3fe33087a7aec963cc135f53ae) Thanks [@trivikr](https://github.com/trivikr)! - Add link to migration guide in the error on jest.enableAutomock() 32 | 33 | - [#113](https://github.com/trivikr/vitest-codemod/pull/113) [`8c28866`](https://github.com/trivikr/vitest-codemod/commit/8c28866f877daa9c4aa9b06ef4c2a071bc22ada4) Thanks [@trivikr](https://github.com/trivikr)! - Delete jest.disableAutomock() as vitest does not mock by default 34 | 35 | ## 0.4.0 36 | 37 | ### Minor Changes 38 | 39 | - [#100](https://github.com/trivikr/vitest-codemod/pull/100) [`76a430f`](https://github.com/trivikr/vitest-codemod/commit/76a430f2d001e161e8b4b8f4109e6c13eaeb96ab) Thanks [@trivikr](https://github.com/trivikr)! - Support Jest APIs which change to async in Vitest 40 | 41 | ### Patch Changes 42 | 43 | - [#98](https://github.com/trivikr/vitest-codemod/pull/98) [`8a1a1c6`](https://github.com/trivikr/vitest-codemod/commit/8a1a1c6dde6459ffa531363d5903b84af17ec253) Thanks [@trivikr](https://github.com/trivikr)! - Add transformation for jest.requireMock 44 | 45 | - [#95](https://github.com/trivikr/vitest-codemod/pull/95) [`dd88dc7`](https://github.com/trivikr/vitest-codemod/commit/dd88dc78f40b5854429cc439569a8f69220bd8ae) Thanks [@trivikr](https://github.com/trivikr)! - Add transformation for jest.createMockFromModule 46 | 47 | - [#99](https://github.com/trivikr/vitest-codemod/pull/99) [`8be7c13`](https://github.com/trivikr/vitest-codemod/commit/8be7c130950e821abed73810acc2c216674bb80d) Thanks [@trivikr](https://github.com/trivikr)! - Add transformation for jest.requireActual 48 | 49 | ## 0.3.3 50 | 51 | ### Patch Changes 52 | 53 | - [#91](https://github.com/trivikr/vitest-codemod/pull/91) [`bb58f74`](https://github.com/trivikr/vitest-codemod/commit/bb58f7452e2d641cc2a4b2c39128fff43b66e2c2) Thanks [@trivikr](https://github.com/trivikr)! - Throw error on jest automocking APIs 54 | 55 | ## 0.3.2 56 | 57 | ### Patch Changes 58 | 59 | - [#87](https://github.com/trivikr/vitest-codemod/pull/87) [`a4aa362`](https://github.com/trivikr/vitest-codemod/commit/a4aa362744ce81b28de8bad37cc86223d2ea7240) Thanks [@trivikr](https://github.com/trivikr)! - Transform Jest snapshots created with printBasicPrototype=true 60 | 61 | ## 0.3.1 62 | 63 | ### Patch Changes 64 | 65 | - [#78](https://github.com/trivikr/vitest-codemod/pull/78) [`421dfeb`](https://github.com/trivikr/vitest-codemod/commit/421dfeb7982e1250dd41c9904503ff760d552581) Thanks [@jgoz](https://github.com/jgoz)! - Preserve file comments when adding imports 66 | 67 | ## 0.3.0 68 | 69 | ### Minor Changes 70 | 71 | - [#80](https://github.com/trivikr/vitest-codemod/pull/80) [`13fb202`](https://github.com/trivikr/vitest-codemod/commit/13fb2021003c31d31ae104bab5b5f4c0da5762ae) Thanks [@trivikr](https://github.com/trivikr)! - Support transformation of jest API to vi 72 | 73 | ## 0.2.0 74 | 75 | ### Minor Changes 76 | 77 | - [#72](https://github.com/trivikr/vitest-codemod/pull/72) [`61d4e88`](https://github.com/trivikr/vitest-codemod/commit/61d4e88847b69ea91085da635ea198b35a9eeed1) Thanks [@trivikr](https://github.com/trivikr)! - Import describe when member expression is used in jest 78 | 79 | ## 0.1.4 80 | 81 | ### Patch Changes 82 | 83 | - [#68](https://github.com/trivikr/vitest-codemod/pull/68) [`e102378`](https://github.com/trivikr/vitest-codemod/commit/e102378f8c677d07f838dad5e09b4a55a4617b30) Thanks [@trivikr](https://github.com/trivikr)! - Support transformation of `fit` to `it.only` 84 | 85 | ## 0.1.3 86 | 87 | ### Patch Changes 88 | 89 | - [#64](https://github.com/trivikr/vitest-codemod/pull/64) [`a38362b`](https://github.com/trivikr/vitest-codemod/commit/a38362b99ae58ce94d05655884a36086f7ddf342) Thanks [@trivikr](https://github.com/trivikr)! - Support transformation of `.failing` to `.fails` 90 | 91 | ## 0.1.2 92 | 93 | ### Patch Changes 94 | 95 | - [#56](https://github.com/trivikr/vitest-codemod/pull/56) [`55d5334`](https://github.com/trivikr/vitest-codemod/commit/55d5334fd57703a5d5272ea19c3dfce78fa478d2) Thanks [@trivikr](https://github.com/trivikr)! - Add it/test import if a member function is called 96 | 97 | ## 0.1.1 98 | 99 | ### Patch Changes 100 | 101 | - [#49](https://github.com/trivikr/vitest-codemod/pull/49) [`bc5275b`](https://github.com/trivikr/vitest-codemod/commit/bc5275b75421213251c022d623a08ab8329b09cd) Thanks [@trivikr](https://github.com/trivikr)! - Replace explicit jest global imports 102 | 103 | ## 0.1.0 104 | 105 | ### Minor Changes 106 | 107 | - [#46](https://github.com/trivikr/vitest-codemod/pull/46) [`fc6bbca`](https://github.com/trivikr/vitest-codemod/commit/fc6bbca36632890c09e4f0d4167d152153a1366d) Thanks [@trivikr](https://github.com/trivikr)! - Bump to 0.1.0 as basic transform to support jest has landed 108 | 109 | ## 0.0.5 110 | 111 | ### Patch Changes 112 | 113 | - [#42](https://github.com/trivikr/vitest-codemod/pull/42) [`9a534e3`](https://github.com/trivikr/vitest-codemod/commit/9a534e3bfc0491886cb752b46e769ea9970af272) Thanks [@trivikr](https://github.com/trivikr)! - Add vitest imports for jest globals 114 | 115 | ## 0.0.4 116 | 117 | ### Patch Changes 118 | 119 | - [#34](https://github.com/trivikr/vitest-codemod/pull/34) [`8e60373`](https://github.com/trivikr/vitest-codemod/commit/8e60373e71a0530b86f73e18a171d249fea76ed7) Thanks [@trivikr](https://github.com/trivikr)! - Format code with @antfu/eslint-config rules 120 | 121 | ## 0.0.3 122 | 123 | ### Patch Changes 124 | 125 | - [#30](https://github.com/trivikr/vitest-codemod/pull/30) [`0429895`](https://github.com/trivikr/vitest-codemod/commit/0429895d54d044e1e8f085fb9c5150d635c1f7f2) Thanks [@trivikr](https://github.com/trivikr)! - Use parser provided by callee 126 | 127 | ## 0.0.2 128 | 129 | ### Patch Changes 130 | 131 | - [#26](https://github.com/trivikr/vitest-codemod/pull/26) [`bce0da7`](https://github.com/trivikr/vitest-codemod/commit/bce0da7221212bd13312b065a192d14a29ec40c4) Thanks [@trivikr](https://github.com/trivikr)! - Remove "workspace:" protocol for internal dependencies 132 | 133 | ## 0.0.1 134 | 135 | ### Patch Changes 136 | 137 | - [#19](https://github.com/trivikr/vitest-codemod/pull/19) [`fbf7743`](https://github.com/trivikr/vitest-codemod/commit/fbf7743d28b070c8b570d80457cfaf68ebbae432) Thanks [@trivikr](https://github.com/trivikr)! - Add basic source code for @vitest-codemod/jest 138 | --------------------------------------------------------------------------------