diff --git a/jest.runner.config.js b/jest.runner.config.js index 101c492e..12000da5 100644 --- a/jest.runner.config.js +++ b/jest.runner.config.js @@ -2,7 +2,7 @@ module.exports = { verbose: true, modulePathIgnorePatterns: ['package.json'], transform: { - '^.+\\.[t|j]sx?$': 'babel-jest', + '^.+\\.[t|j]sx?$': '/jest.transformer.js', }, reporters: [], } diff --git a/jest.transformer.js b/jest.transformer.js new file mode 100644 index 00000000..d56b5f56 --- /dev/null +++ b/jest.transformer.js @@ -0,0 +1,49 @@ +// Transforms solutions and test files for Jest. +// +// Most files go through SWC. It produces the same CommonJS as Babel, but it +// is written in Rust and loads and runs much faster than Babel. +// +// Babel still handles two cases, so that every test passes or fails exactly +// as it did with Babel alone: +// +// - Files that call jest.mock (or a related call). SWC does not move these +// calls above the imports when `jest` is imported from '@jest/globals'. +// - Files that SWC fails to compile. Babel then compiles them, or reports the +// same syntax error as before. +// +// Babel is only loaded when one of these cases comes up. +const swcJest = require('@swc/jest') + +const HOISTED_JEST_CALL = + /\bjest\s*\.\s*(mock|unmock|enableAutomock|disableAutomock|deepUnmock)\s*\(/ + +const swc = swcJest.createTransformer({ + swcrc: false, + env: { + targets: { node: process.versions.node }, + mode: 'usage', + coreJs: '3.44', + }, +}) + +let babel = null +function getBabel() { + if (!babel) { + babel = require('babel-jest').default.createTransformer() + } + return babel +} + +module.exports = { + process(sourceText, sourcePath, options) { + if (HOISTED_JEST_CALL.test(sourceText)) { + return getBabel().process(sourceText, sourcePath, options) + } + + try { + return swc.process(sourceText, sourcePath, options) + } catch { + return getBabel().process(sourceText, sourcePath, options) + } + }, +} diff --git a/package.json b/package.json index 0c3ee3db..ab4162ba 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,8 @@ "@jest/reporters": "^30.4.0", "@jest/test-result": "^30.4.0", "@jest/types": "^30.4.0", + "@swc/core": "^1.16.22", + "@swc/jest": "^0.2.39", "@typescript-eslint/typescript-estree": "^8.37.0", "@typescript-eslint/visitor-keys": "^8.37.0", "babel-jest": "^30.4.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b91f1a2a..69f88d66 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -26,6 +26,12 @@ importers: '@jest/types': specifier: ^30.4.0 version: 30.5.1 + '@swc/core': + specifier: ^1.16.22 + version: 1.16.22 + '@swc/jest': + specifier: ^0.2.39 + version: 0.2.39(@swc/core@1.16.22) '@typescript-eslint/typescript-estree': specifier: ^8.37.0 version: 8.63.0(typescript@5.7.3) @@ -832,6 +838,10 @@ packages: node-notifier: optional: true + '@jest/create-cache-key-function@30.5.1': + resolution: {integrity: sha512-5qkif//qhlbt54255O/M2JtUZNlnefOzE12bi7FlApOIYO4pqrZcAwhNCfP4jOGFubez8uoLgY9czWFjYY57ow==} + engines: {node: ^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0} + '@jest/diff-sequences@30.5.0': resolution: {integrity: sha512-OsqBjHXCn8cadasoAZBP6nWYvMsRhpMzGXTpxJ5aO04NlbdhIz+FVe3q49l0AwVhsz/cEmIpBes6gAFl1/dWQg==} engines: {node: ^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0} @@ -1054,6 +1064,99 @@ packages: '@sinonjs/fake-timers@15.4.0': resolution: {integrity: sha512-DsG+8/LscQIQg68J6Ef3dv10u6nVyetYn923s3/sus5eaGfTo1of5WMZSLf0UJc9KDuKPilPH0UDJCjvNbDNCA==} + '@swc/core-darwin-arm64@1.16.22': + resolution: {integrity: sha512-PddkOjOK3MK01AVOvqG309iAJ0hn5vK37ug9g5Zt5e2aWF3IhVLdDLjMePXts1zi8yc4ild2h9FYCkVYb5+Qcw==} + engines: {node: '>=10'} + cpu: [arm64] + os: [darwin] + + '@swc/core-darwin-x64@1.16.22': + resolution: {integrity: sha512-Zzia5cuIQFLTfZyf5mC1JvRMPW1VlxbvkZ0qNESoD4okE6T6Us2ZNVZ5XMVELqkYNw9eL2GeuMJgDIHBdYo7Gg==} + engines: {node: '>=10'} + cpu: [x64] + os: [darwin] + + '@swc/core-linux-arm-gnueabihf@1.16.22': + resolution: {integrity: sha512-GI6VICiuYc7mb61XenLtiEGoOXTXDdUda8w0bu29Mh3Z6rhuTkAiBAdDFpdww2QNV0iACY5ylUSL127I6Nx8yQ==} + engines: {node: '>=10'} + cpu: [arm] + os: [linux] + + '@swc/core-linux-arm64-gnu@1.16.22': + resolution: {integrity: sha512-Ic+qrMmV78QdlG9irJuc/37xSqY7+5ZFhg4kaYbF82UQkaEfWgqGSUbU75Y4To21eaBB07c05kbXmTWTrvPURA==} + engines: {node: '>=10'} + cpu: [arm64] + os: [linux] + + '@swc/core-linux-arm64-musl@1.16.22': + resolution: {integrity: sha512-ophleEwlj0SqkzYPDJ7xMPK0+MnWfc4+u9GzDYLLFG1TkIJuwj0rHNWto4MmKo00TylHFwGx9+5bLs/+YZ00IQ==} + engines: {node: '>=10'} + cpu: [arm64] + os: [linux] + + '@swc/core-linux-ppc64-gnu@1.16.22': + resolution: {integrity: sha512-/4kR7SafFsVqY8N4HSepTs4Ni9YA0OY6n/SVBk/V15PhDiw97stG9SVqvgc8EzC+3XOe4pTmwlvFLWzNYfuNWg==} + engines: {node: '>=10'} + cpu: [ppc64] + os: [linux] + + '@swc/core-linux-s390x-gnu@1.16.22': + resolution: {integrity: sha512-b3GQqKmHBHUFQby1sh/QLtNQMMXTSBUhnmXcJ5qSrWbkYnhljIW3Y+30eMrrxCfy7HXLLDug1sjlhJDhf3tPhA==} + engines: {node: '>=10'} + cpu: [s390x] + os: [linux] + + '@swc/core-linux-x64-gnu@1.16.22': + resolution: {integrity: sha512-u9hdfNEI3oI/dTRJ+In8G32EgfFpZDvMoI5elIERl6/XfaIzE9gpv4WomhKblAXA1+EYyLfDCS1EFFKQlBXTQQ==} + engines: {node: '>=10'} + cpu: [x64] + os: [linux] + + '@swc/core-linux-x64-musl@1.16.22': + resolution: {integrity: sha512-C3YVRL717kKSSU8n6HaC7sesFSGnHjRuHN3609Wl4epPYVDDutzD6aj7CMcx9cg3WWJ/oE8woDZ8qrCfzmxvnw==} + engines: {node: '>=10'} + cpu: [x64] + os: [linux] + + '@swc/core-win32-arm64-msvc@1.16.22': + resolution: {integrity: sha512-+MpNuE45LMFo+6RF8vMUqZPmaOfRQIuzCUHJ+jSXwEhiR5KHeZiQ9MaJglZRDeMuML67voCiFLrxqYTqGCbcCQ==} + engines: {node: '>=10'} + cpu: [arm64] + os: [win32] + + '@swc/core-win32-ia32-msvc@1.16.22': + resolution: {integrity: sha512-z1p/CMDSRqvsE5Gs7axHVl6p+2zjoQTYuGcZ/q+3PMjAxx2bo/+lkpG9ot+725e6lj9os3WwYQrncmOWKVDrEQ==} + engines: {node: '>=10'} + cpu: [ia32] + os: [win32] + + '@swc/core-win32-x64-msvc@1.16.22': + resolution: {integrity: sha512-IyBPJn/SpXsh7i5ZLfQUPJ+R+fuELPI7Sz51igmYMTv2wdnDn/tqnIlAUwQe6AgnclbuJdbOb6crUbyN/zwMzw==} + engines: {node: '>=10'} + cpu: [x64] + os: [win32] + + '@swc/core@1.16.22': + resolution: {integrity: sha512-kFqhtSmmNSZqJXIK0l9rF+oNg2efaZoeNbN/4qwbNTWfMRKApc6n7rKx827bw8K+4kmu7iH0LZ144QJTQzulHw==} + engines: {node: '>=10'} + peerDependencies: + '@swc/helpers': '>=0.5.17' + peerDependenciesMeta: + '@swc/helpers': + optional: true + + '@swc/counter@0.1.3': + resolution: {integrity: sha512-e2BR4lsJkkRlKZ/qCHPw9ZaSxc0MVUd7gtbtaB7aMvHeJVYe8sOB8DBZkP2DtISHGSku9sCK6T6cnY0CtXrOCQ==} + + '@swc/jest@0.2.39': + resolution: {integrity: sha512-eyokjOwYd0Q8RnMHri+8/FS1HIrIUKK/sRrFp8c1dThUOfNeCWbLmBP1P5VsKdvmkd25JaH+OKYwEYiAYg9YAA==} + engines: {npm: '>= 7.0.0'} + peerDependencies: + '@swc/core': '*' + + '@swc/types@0.1.28': + resolution: {integrity: sha512-V6Mnml8v09QALx6K0elJ7o9K/MkVDtW3t6L+7Ou/JcWtb3xwId2AH4FeOceySd2JaO87IMw4+6vSZxLm34LPbw==} + '@tsconfig/node22@22.0.6': resolution: {integrity: sha512-/5thavHAnWDZwH2H6eEn/T8pBEBhtuKaWGMslsj82kJfvzQgOgEMK7X4goBbIUjgVtAzPTtM9Ml64LA0uxO6Iw==} @@ -2355,6 +2458,9 @@ packages: engines: {node: '>=6'} hasBin: true + jsonc-parser@3.3.1: + resolution: {integrity: sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ==} + keyv@4.5.4: resolution: {integrity: sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==} @@ -4025,6 +4131,10 @@ snapshots: - supports-color - ts-node + '@jest/create-cache-key-function@30.5.1': + dependencies: + '@jest/types': 30.5.1 + '@jest/diff-sequences@30.5.0': {} '@jest/environment@30.5.0': @@ -4333,6 +4443,73 @@ snapshots: dependencies: '@sinonjs/commons': 3.0.1 + '@swc/core-darwin-arm64@1.16.22': + optional: true + + '@swc/core-darwin-x64@1.16.22': + optional: true + + '@swc/core-linux-arm-gnueabihf@1.16.22': + optional: true + + '@swc/core-linux-arm64-gnu@1.16.22': + optional: true + + '@swc/core-linux-arm64-musl@1.16.22': + optional: true + + '@swc/core-linux-ppc64-gnu@1.16.22': + optional: true + + '@swc/core-linux-s390x-gnu@1.16.22': + optional: true + + '@swc/core-linux-x64-gnu@1.16.22': + optional: true + + '@swc/core-linux-x64-musl@1.16.22': + optional: true + + '@swc/core-win32-arm64-msvc@1.16.22': + optional: true + + '@swc/core-win32-ia32-msvc@1.16.22': + optional: true + + '@swc/core-win32-x64-msvc@1.16.22': + optional: true + + '@swc/core@1.16.22': + dependencies: + '@swc/counter': 0.1.3 + '@swc/types': 0.1.28 + optionalDependencies: + '@swc/core-darwin-arm64': 1.16.22 + '@swc/core-darwin-x64': 1.16.22 + '@swc/core-linux-arm-gnueabihf': 1.16.22 + '@swc/core-linux-arm64-gnu': 1.16.22 + '@swc/core-linux-arm64-musl': 1.16.22 + '@swc/core-linux-ppc64-gnu': 1.16.22 + '@swc/core-linux-s390x-gnu': 1.16.22 + '@swc/core-linux-x64-gnu': 1.16.22 + '@swc/core-linux-x64-musl': 1.16.22 + '@swc/core-win32-arm64-msvc': 1.16.22 + '@swc/core-win32-ia32-msvc': 1.16.22 + '@swc/core-win32-x64-msvc': 1.16.22 + + '@swc/counter@0.1.3': {} + + '@swc/jest@0.2.39(@swc/core@1.16.22)': + dependencies: + '@jest/create-cache-key-function': 30.5.1 + '@swc/core': 1.16.22 + '@swc/counter': 0.1.3 + jsonc-parser: 3.3.1 + + '@swc/types@0.1.28': + dependencies: + '@swc/counter': 0.1.3 + '@tsconfig/node22@22.0.6': {} '@types/babel__core@7.20.5': @@ -6002,6 +6179,8 @@ snapshots: json5@2.2.3: {} + jsonc-parser@3.3.1: {} + keyv@4.5.4: dependencies: json-buffer: 3.0.1 diff --git a/test/fixtures/two-fer/fail/empty/expected_results.json b/test/fixtures/two-fer/fail/empty/expected_results.json index 527649c5..ebd3b575 100644 --- a/test/fixtures/two-fer/fail/empty/expected_results.json +++ b/test/fixtures/two-fer/fail/empty/expected_results.json @@ -4,21 +4,21 @@ { "name": "twoFer() > no name given", "status": "fail", - "message": "TypeError: (0 , _twoFer.twoFer) is not a function", + "message": "TypeError: (0 , _twofer.twoFer) is not a function", "output": null, "test_code": "expect(twoFer()).toEqual('One for you, one for me.');" }, { "name": "twoFer() > a name given", "status": "fail", - "message": "TypeError: (0 , _twoFer.twoFer) is not a function", + "message": "TypeError: (0 , _twofer.twoFer) is not a function", "output": null, "test_code": "const name = 'Alice';\nexpect(twoFer(name)).toEqual('One for Alice, one for me.');" }, { "name": "twoFer() > another name given", "status": "fail", - "message": "TypeError: (0 , _twoFer.twoFer) is not a function", + "message": "TypeError: (0 , _twofer.twoFer) is not a function", "output": null, "test_code": "const name = 'Bob';\nexpect(twoFer(name)).toEqual('One for Bob, one for me.');" } diff --git a/test/fixtures/two-fer/fail/tests/expected_results.json b/test/fixtures/two-fer/fail/tests/expected_results.json index b36033d9..76ca7430 100644 --- a/test/fixtures/two-fer/fail/tests/expected_results.json +++ b/test/fixtures/two-fer/fail/tests/expected_results.json @@ -5,14 +5,14 @@ "name": "twoFer() > no name given", "status": "pass", "message": "", - "output": "[log] undefined", + "output": "[log] Some global log\n[log] undefined", "test_code": "expect(twoFer()).toEqual('One for you, one for me.');" }, { "name": "twoFer() > a name given", "status": "fail", "message": "Error: \u001b[2mexpect(\u001b[22m\u001b[31mreceived\u001b[39m\u001b[2m).\u001b[22mtoEqual\u001b[2m(\u001b[22m\u001b[32mexpected\u001b[39m\u001b[2m) // deep equality\u001b[22m\n\nExpected: \u001b[32m\"One for \u001b[7mAlice\u001b[27m, one for me.\"\u001b[39m\nReceived: \u001b[31m\"One for \u001b[7myou\u001b[27m, one for me.\"\u001b[39m", - "output": "[log] Some global log\n[log] Alice", + "output": "[log] Alice", "test_code": "const name = 'Alice';\nexpect(twoFer(name)).toEqual('One for Alice, one for me.');" }, {