Skip to content

Commit b58d089

Browse files
authored
feat: add an option to configure which file processed by esbuild (#1455)
Fixes #1413 Fixes #1437 BREAKING CHANGE Previously, we always checked file extension `.mjs` and any files from `node_modules` excluding `tslib` to be processed with `esbuild`. With the new option `processWithEsbuild`, now we put default all `.mjs` files to be processed by `esbuild`. Files like `lodash-es` default isn't processed by `esbuild`. If you wish to use `esbuild` to process such files, please configure in your Jest config like ``` // jest.config.js module.exports = { //... globals: { ngJest: { processWithEsbuild: ['**/node_modules/lodash-es/*.js], } } } ```
1 parent f3e97d7 commit b58d089

18 files changed

+168
-65
lines changed

.eslintignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
build/
2-
node_modules/
1+
build
2+
dist
3+
node_modules
34
src/config/setup-jest.ts
4-
coverage/
5+
coverage
56
website
67
src/transformers/downlevel_decorators_transform
78
src/ngtsc

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ website/.yarn/*
1414
!.yarn/versions
1515
!/e2e/full-ivy-lib/node_modules
1616
!/e2e/partial-ivy-lib/node_modules
17-
!/e2e/process-js-packages/node_modules
1817
src/transformers/downlevel_decorators_transform
1918
src/ngtsc
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import fs from 'fs';
21
import path from 'path';
32

4-
import { jsonNoCache as runWithJsonNoCache } from '../run-jest';
3+
import { onNodeVersions, jsonNoCache as runWithJsonNoCache } from '../run-jest';
4+
import { runYarnInstall } from '../utils';
55

6-
const TEST_DIR_NAME = 'process-js-packages';
7-
const TEST_DIR_PATH = path.join(__dirname, '..', TEST_DIR_NAME);
8-
const LOG_FILE_NAME = 'ng-jest.log';
9-
const LOG_FILE_PATH = path.join(TEST_DIR_PATH, LOG_FILE_NAME);
6+
const DIR = 'process-js-packages';
107

11-
test(`successfully runs the tests inside ${TEST_DIR_NAME}`, () => {
12-
process.env.NG_JEST_LOG = LOG_FILE_NAME;
8+
beforeAll(() => {
9+
runYarnInstall(path.join(__dirname, '..', DIR));
10+
});
1311

14-
const { json } = runWithJsonNoCache(TEST_DIR_NAME);
12+
test(`successfully run the tests inside ${DIR} with CommonJS mode`, () => {
13+
const { json } = runWithJsonNoCache(DIR);
1514

1615
expect(json.success).toBe(true);
17-
expect(fs.existsSync(LOG_FILE_PATH)).toBe(true);
18-
19-
const logFileContent = fs.readFileSync(LOG_FILE_PATH, 'utf-8');
20-
const logFileContentAsJson = JSON.parse(logFileContent);
16+
});
2117

22-
expect(/node_modules\/(.*.m?js$)/.test(logFileContentAsJson.context.filePath.replace(/\\/g, '/'))).toBe(true);
23-
expect(logFileContentAsJson.message).toBe('process with esbuild');
18+
// The versions where vm.Module exists and commonjs with "exports" is not broken
19+
onNodeVersions('>=12.16.0', () => {
20+
test(`successfully run the tests inside ${DIR} with ESM mode`, () => {
21+
const { json } = runWithJsonNoCache(DIR, ['-c=jest-esm.config.mjs'], {
22+
nodeOptions: '--experimental-vm-modules --no-warnings',
23+
});
2424

25-
delete process.env.NG_JEST_LOG;
26-
fs.unlinkSync(LOG_FILE_PATH);
25+
expect(json.success).toBe(true);
26+
});
2727
});
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import { foo } from 'my-lib';
1+
import { MarkerClusterer } from '@googlemaps/markerclusterer';
2+
import camelCase from 'lodash-es/camelCase';
3+
import { __assign } from 'tslib';
24

35
test('should pass', () => {
4-
expect(foo).toBe(1);
6+
expect(typeof MarkerClusterer).toBe('function');
7+
expect(camelCase('foo-bar')).toBe('fooBar');
8+
expect(typeof __assign).toBe('function');
59
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default {
2+
extensionsToTreatAsEsm: ['.ts'],
3+
globals: {
4+
'ts-jest': {
5+
tsconfig: 'tsconfig-esm.spec.json',
6+
useESM: true,
7+
isolatedModules: true,
8+
},
9+
},
10+
transform: {
11+
'^.+\\.(ts|js)$': '<rootDir>/../../build/index.js',
12+
},
13+
moduleNameMapper: {
14+
'@googlemaps/markerclusterer': '@googlemaps/markerclusterer/dist/index.esm.js',
15+
},
16+
transformIgnorePatterns: ['node_modules/(?!@googlemaps/markerclusterer)'],
17+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
globals: {
3+
'ts-jest': {
4+
tsconfig: 'tsconfig.spec.json',
5+
isolatedModules: true,
6+
},
7+
ngJest: {
8+
processWithEsbuild: ['**/node_modules/lodash-es/*.js'],
9+
},
10+
},
11+
transform: {
12+
'^.+\\.(ts|js|mjs)$': '<rootDir>/../../build/index.js',
13+
},
14+
transformIgnorePatterns: ['node_modules/(?!lodash-es)'],
15+
};

e2e/process-js-packages/node_modules/my-lib/index.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

e2e/process-js-packages/node_modules/my-lib/index.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

e2e/process-js-packages/node_modules/my-lib/package.json

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
{
22
"name": "process-js-packages",
3-
"jest": {
4-
"moduleFileExtensions": ["ts", "html", "js", "json", "mjs"],
5-
"globals": {
6-
"ts-jest": {
7-
"isolatedModules": true
8-
}
9-
},
10-
"transform": {
11-
"^.+\\.(ts|js|mjs|html)$": "<rootDir>/../../build/index.js"
12-
},
13-
"transformIgnorePatterns": ["node_modules/(?!my-lib)"]
3+
"private": true,
4+
"devDependencies": {
5+
"lodash-es": "^4.17.21",
6+
"@types/lodash-es": "^4.17.6",
7+
"tslib": "^2.4.0",
8+
"@googlemaps/markerclusterer": "^2.0.6"
149
}
1510
}

0 commit comments

Comments
 (0)