Skip to content

Commit

Permalink
fix(react): handle transform properly for js/ts files in @nrwl/react/…
Browse files Browse the repository at this point in the history
…plugin/jest (#9898)

Fixes #9640
  • Loading branch information
jaysoo authored Apr 20, 2022
1 parent e32932d commit 8171e68
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"@svgr/webpack": "^6.1.2",
"@swc-node/register": "^1.4.2",
"@swc/core": "^1.2.152",
"@swc/jest": "^0.2.20",
"@testing-library/react": "11.2.6",
"@testing-library/react-hooks": "7.0.1",
"@tippyjs/react": "^4.2.6",
Expand Down
24 changes: 23 additions & 1 deletion packages/react/plugins/jest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as path from 'path';
import { names } from '@nrwl/devkit';

const JS_SOURCE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs'];

module.exports = {
process(src, filename) {
process(src, filename, options) {
const assetFilename = JSON.stringify(path.basename(filename));

if (filename.match(/\.svg$/)) {
Expand All @@ -28,6 +30,26 @@ module.exports = {
};`;
}

if (JS_SOURCE_EXTENSIONS.includes(path.extname(filename))) {
const transformer = getJsTransform();
if (transformer) return transformer.process(src, filename, options);
}

// Fallback for unknown extensions
return `module.exports = ${assetFilename};`;
},
};

function getJsTransform() {
try {
return require('babel-jest').default;
} catch {
// ignored
}

try {
return require('@swc/jest').createTransformer();
} catch {
// ignored
}
}
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,13 @@
slash "^3.0.0"
strip-ansi "^6.0.0"

"@jest/create-cache-key-function@^27.4.2":
version "27.5.1"
resolved "https://registry.yarnpkg.com/@jest%2fcreate-cache-key-function/-/create-cache-key-function-27.5.1.tgz#7448fae15602ea95c828f5eceed35c202a820b31"
integrity sha512-dmH1yW+makpTSURTy8VzdUwFnfQh1G8R+DxO2Ho2FFmBbKFEVm+3jWdvFhE2VqB/LATCTokkP0dotjyQyw5/AQ==
dependencies:
"@jest/types" "^27.5.1"

"@jest/environment@^27.2.3", "@jest/environment@^27.5.1":
version "27.5.1"
resolved "https://registry.yarnpkg.com/@jest/environment/-/environment-27.5.1.tgz#d7425820511fe7158abbecc010140c3fd3be9c74"
Expand Down Expand Up @@ -4343,6 +4350,13 @@
"@swc/core-win32-ia32-msvc" "1.2.160"
"@swc/core-win32-x64-msvc" "1.2.160"

"@swc/jest@^0.2.20":
version "0.2.20"
resolved "https://registry.yarnpkg.com/@swc%2fjest/-/jest-0.2.20.tgz#2bddb4348fb730296b86cdcd96748be131b11395"
integrity sha512-5qSUBYY1wyIMn7p0Vl9qqV4hMI69oJwZCIPUpBsTFWN2wlwn6RDugzdgCn+bLXVYh+Cxi8bJcZ1uumDgsoL+FA==
dependencies:
"@jest/create-cache-key-function" "^27.4.2"

"@szmarczak/http-timer@^1.1.2":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@szmarczak/http-timer/-/http-timer-1.1.2.tgz#b1665e2c461a2cd92f4c1bbf50d5454de0d4b421"
Expand Down Expand Up @@ -14978,6 +14992,9 @@ levn@~0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/levn/-/levn-0.3.0.tgz#3b09924edf9f083c0490fdd4c0bc4421e04764ee"
integrity sha1-OwmSTt+fCDwEkP3UwLxEIeBHZO4=
dependencies:
prelude-ls "~1.1.2"
type-check "~0.3.2"

license-webpack-plugin@4.0.2, license-webpack-plugin@^4.0.2:
version "4.0.2"
Expand Down

1 comment on commit 8171e68

@vercel
Copy link

@vercel vercel bot commented on 8171e68 Apr 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-dev-nrwl.vercel.app
nx-five.vercel.app

Please sign in to comment.