Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"sourceType": "module",
"project": "./tsconfig.json"
},
"env": {
"node": true
},
"ignorePatterns": ["**/*"],
"plugins": ["@typescript-eslint", "@nrwl/nx"],
"extends": [
Expand Down
4 changes: 2 additions & 2 deletions docs/angular/api-jest/builders/jest.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Run all tests serially in the current process (rather than creating a worker poo

Type: `string`

The name of a setup file used by Jest. (https://jestjs.io/docs/en/configuration#setupfilesafterenv-array)
[Deprecated] The name of a setup file used by Jest. (use Jest config file https://jestjs.io/docs/en/configuration#setupfilesafterenv-array)

### showConfig

Expand Down Expand Up @@ -186,7 +186,7 @@ Node module that implements a custom results processor. (https://jestjs.io/docs/

Type: `string`

The name of the Typescript configuration file.
[Deprecated] The name of the Typescript configuration file. Set the tsconfig option in the jest config file.

### updateSnapshot

Expand Down
4 changes: 2 additions & 2 deletions docs/react/api-jest/builders/jest.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Run all tests serially in the current process (rather than creating a worker poo

Type: `string`

The name of a setup file used by Jest. (https://jestjs.io/docs/en/configuration#setupfilesafterenv-array)
[Deprecated] The name of a setup file used by Jest. (use Jest config file https://jestjs.io/docs/en/configuration#setupfilesafterenv-array)

### showConfig

Expand Down Expand Up @@ -187,7 +187,7 @@ Node module that implements a custom results processor. (https://jestjs.io/docs/

Type: `string`

The name of the Typescript configuration file.
[Deprecated] The name of the Typescript configuration file. Set the tsconfig option in the jest config file.

### updateSnapshot

Expand Down
5 changes: 5 additions & 0 deletions e2e/node/src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ forEachCli((currentCLIName) => {
stripIndents`module.exports = {
name: '${nestlib}',
preset: '../../jest.config.js',
globals: {
'ts-jest': {
tsConfig: '<rootDir>/tsconfig.spec.json',
},
},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
Expand Down
8 changes: 8 additions & 0 deletions packages/jest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export {
addPropertyToJestConfig,
removePropertyFromJestConfig,
} from './src/utils/config/update-config';
export {
jestConfigObjectAst,
jestConfigObject,
} from './src/utils/config/functions';
5 changes: 5 additions & 0 deletions packages/jest/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"version": "9.2.0-beta.3",
"description": "Update jest to v25",
"factory": "./src/migrations/update-9-2-0/update-9-2-0"
},
"update-10.0.0": {
"version": "10.0.0-beta.2",
"description": "update jest configs to include setup env files",
"factory": "./src/migrations/update-10-0-0/update-jest-configs"
}
},
"packageJsonUpdates": {
Expand Down
73 changes: 0 additions & 73 deletions packages/jest/src/builders/jest/jest.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ describe('Jest Builder', () => {
expect(runCLI).toHaveBeenCalledWith(
{
_: [],
globals: JSON.stringify({
'ts-jest': {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer',
],
},
}),
testPathPattern: [],
watch: false,
},
Expand Down Expand Up @@ -103,16 +93,6 @@ describe('Jest Builder', () => {
expect(runCLI).toHaveBeenCalledWith(
{
_: ['lib.spec.ts'],
globals: JSON.stringify({
'ts-jest': {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer',
],
},
}),
coverage: false,
runInBand: true,
testNamePattern: 'should load',
Expand Down Expand Up @@ -147,16 +127,6 @@ describe('Jest Builder', () => {
expect(runCLI).toHaveBeenCalledWith(
{
_: ['file1.ts', 'file2.ts'],
globals: JSON.stringify({
'ts-jest': {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer',
],
},
}),
coverage: false,
findRelatedTests: true,
runInBand: true,
Expand Down Expand Up @@ -206,16 +176,6 @@ describe('Jest Builder', () => {
expect(runCLI).toHaveBeenCalledWith(
{
_: [],
globals: JSON.stringify({
'ts-jest': {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer',
],
},
}),
coverage: true,
bail: 1,
color: false,
Expand Down Expand Up @@ -260,16 +220,6 @@ describe('Jest Builder', () => {
expect(runCLI).toHaveBeenCalledWith(
{
_: [],
globals: JSON.stringify({
'ts-jest': {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer',
],
},
}),
maxWorkers: '50%',
testPathPattern: [],
},
Expand All @@ -292,16 +242,6 @@ describe('Jest Builder', () => {
expect(runCLI).toHaveBeenCalledWith(
{
_: [],
globals: JSON.stringify({
'ts-jest': {
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer',
],
},
}),
setupFilesAfterEnv: ['/root/test-setup.ts'],
testPathPattern: [],
watch: false,
Expand Down Expand Up @@ -350,18 +290,6 @@ describe('Jest Builder', () => {
expect(runCLI).toHaveBeenCalledWith(
{
_: [],
globals: JSON.stringify({
hereToStay: true,
'ts-jest': {
diagnostics: false,
tsConfig: '/root/tsconfig.test.json',
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer',
],
},
}),
setupFilesAfterEnv: ['/root/test-setup.ts'],
testPathPattern: [],
watch: false,
Expand Down Expand Up @@ -400,7 +328,6 @@ describe('Jest Builder', () => {
expect(runCLI).toHaveBeenCalledWith(
{
_: [],
globals: '{}',
testPathPattern: [],
watch: false,
},
Expand Down
49 changes: 13 additions & 36 deletions packages/jest/src/builders/jest/jest.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { JestBuilderOptions } from './schema';

try {
require('dotenv').config();
} catch (e) {}
} catch (e) {
// noop
}

if (process.env.NODE_ENV == null || process.env.NODE_ENV == undefined) {
(process.env as any).NODE_ENV = 'test';
}

export default createBuilder<JestBuilderOptions>(run);

function run(
options: JestBuilderOptions,
context: BuilderContext
Expand All @@ -28,45 +28,18 @@ function run(
const jestConfig: {
transform: any;
globals: any;
setupFilesAfterEnv: any;
// eslint-disable-next-line @typescript-eslint/no-var-requires
} = require(options.jestConfig);

let transformers = Object.values<string>(jestConfig.transform || {});
const transformers = Object.values<string>(jestConfig.transform || {});
if (transformers.includes('babel-jest') && transformers.includes('ts-jest')) {
throw new Error(
'Using babel-jest and ts-jest together is not supported.\n' +
'See ts-jest documentation for babel integration: https://kulshekhar.github.io/ts-jest/user/config/babelConfig'
);
}

// use ts-jest by default
const globals = jestConfig.globals || {};
if (!transformers.includes('babel-jest')) {
const tsJestConfig = {
tsConfig: path.resolve(context.workspaceRoot, options.tsConfig),
};

// TODO: This is hacky, We should probably just configure it in the user's workspace
// If jest-preset-angular is installed, apply settings
try {
require.resolve('jest-preset-angular');
Object.assign(tsJestConfig, {
stringifyContentPathRegex: '\\.(html|svg)$',
astTransformers: [
'jest-preset-angular/build/InlineFilesTransformer',
'jest-preset-angular/build/StripStylesTransformer',
],
});
} catch (e) {}

// merge the jestConfig globals with our 'ts-jest' override
Object.assign(globals, {
'ts-jest': {
...(globals['ts-jest'] || {}),
...tsJestConfig,
},
});
}

const config: any = {
_: [],
config: options.config,
Expand Down Expand Up @@ -95,13 +68,15 @@ function run(
useStderr: options.useStderr,
watch: options.watch,
watchAll: options.watchAll,
globals: JSON.stringify(globals),
};

// for backwards compatibility
if (options.setupFile) {
config.setupFilesAfterEnv = [
const setupFilesAfterEnvSet = new Set([
...(jestConfig.setupFilesAfterEnv ?? []),
path.resolve(context.workspaceRoot, options.setupFile),
];
]);
config.setupFilesAfterEnv = Array.from(setupFilesAfterEnvSet);
}

if (options.testFile) {
Expand Down Expand Up @@ -132,3 +107,5 @@ function run(
})
);
}

export default createBuilder<JestBuilderOptions>(run);
12 changes: 7 additions & 5 deletions packages/jest/src/builders/jest/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
"type": "string"
},
"tsConfig": {
"description": "The name of the Typescript configuration file.",
"type": "string"
"description": "[Deprecated] The name of the Typescript configuration file. Set the tsconfig option in the jest config file. ",
"type": "string",
"x-deprecated": true
},
"setupFile": {
"description": "The name of a setup file used by Jest. (https://jestjs.io/docs/en/configuration#setupfilesafterenv-array)",
"type": "string"
"description": "[Deprecated] The name of a setup file used by Jest. (use Jest config file https://jestjs.io/docs/en/configuration#setupfilesafterenv-array)",
"type": "string",
"x-deprecated": true
},
"bail": {
"alias": "b",
Expand Down Expand Up @@ -150,5 +152,5 @@
"type": "boolean"
}
},
"required": ["jestConfig", "tsConfig"]
"required": ["jestConfig"]
}
Loading