Skip to content
Open
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
7 changes: 7 additions & 0 deletions examples/jest/simple-app/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@
"zoneless": false
}
},
"test-inline-config": {
"builder": "@angular-builders/jest:run",
"options": {
"config": { "testTimeout": 10000, "verbose": true },
"zoneless": false
}
},
"e2e": {
"builder": "@cypress/schematic:cypress",
"options": {
Expand Down
1 change: 1 addition & 0 deletions examples/jest/simple-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test": "ng test",
"test:ts": "ng run simple-app:test-ts-config",
"test:esm": "ng run simple-app:test-esm-config",
"test:inline-config": "ng run simple-app:test-inline-config",
"lint": "ng lint",
"e2e": "ng e2e",
"cypress:open": "cypress open",
Expand Down
23 changes: 10 additions & 13 deletions packages/jest/src/custom-config.resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('Resolve project custom config', () => {
expect(customConfig).toEqual({});
});

it('should log a warning when the custom configuration is not found', async () => {
it('should log a warning when an explicitly configured custom configuration is not found', async () => {
existsSyncMock.mockReturnValue(false);
await customConfigResolver.resolveForProject(
normalize('./myproject'),
Expand All @@ -74,6 +74,12 @@ describe('Resolve project custom config', () => {
expect(mockLogger.warn.mock.calls.length).toBe(1);
});

it('should NOT warn when the schema default jest.config.js is absent', async () => {
existsSyncMock.mockReturnValue(false);
await customConfigResolver.resolveForProject(normalize('./myproject'), 'jest.config.js');
expect(mockLogger.warn).not.toHaveBeenCalled();
});

it('Should parse and return inline JSON configuration', async () => {
const inlineConfig = { testTimeout: 10000, verbose: true };
const customConfig = await customConfigResolver.resolveForProject(
Expand All @@ -87,28 +93,19 @@ describe('Resolve project custom config', () => {

it('Should treat non-JSON string as file path', async () => {
existsSyncMock.mockReturnValue(false);
await customConfigResolver.resolveForProject(
normalize('./myproject'),
'jest.config.js'
);
await customConfigResolver.resolveForProject(normalize('./myproject'), 'jest.config.js');
expect(existsSyncMock).toHaveBeenCalled();
});

it('Should treat invalid JSON as file path', async () => {
existsSyncMock.mockReturnValue(false);
await customConfigResolver.resolveForProject(
normalize('./myproject'),
'{invalid json'
);
await customConfigResolver.resolveForProject(normalize('./myproject'), '{invalid json');
expect(existsSyncMock).toHaveBeenCalled();
});

it('Should not treat JSON array as config', async () => {
existsSyncMock.mockReturnValue(false);
await customConfigResolver.resolveForProject(
normalize('./myproject'),
'["item1", "item2"]'
);
await customConfigResolver.resolveForProject(normalize('./myproject'), '["item1", "item2"]');
// Arrays should be treated as file paths, not config objects
expect(existsSyncMock).toHaveBeenCalled();
});
Expand Down
13 changes: 10 additions & 3 deletions packages/jest/src/custom-config.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ export class CustomConfigResolver {
// Treat as file path
const jestConfigPath = getSystemPath(join(projectRoot, configPath));
if (!existsSync(jestConfigPath)) {
this.logger.warn(
`warning: unable to locate custom jest configuration file at path "${jestConfigPath}"`
);
// Only warn when the user explicitly pointed at a missing file.
// 'jest.config.js' is the schema default — best-effort discovery —
// and is legitimately absent when the user keeps config elsewhere
// (package.json `jest`, workspace-level jest.config.*, or an inline
// JestConfig object in angular.json). See #1102.
if (configPath !== 'jest.config.js') {
this.logger.warn(
`warning: unable to locate custom jest configuration file at path "${jestConfigPath}"`
);
}
return {};
}
const tsConfig = getTsConfigPath(projectRoot, this.options);
Expand Down
9 changes: 9 additions & 0 deletions packages/jest/tests/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ module.exports = [
app: 'examples/jest/simple-app',
command: 'yarn test:esm --no-cache',
},
{
id: 'config-inline-object',
name: 'jest: inline config object in angular.json',
purpose:
'Builder runs tests successfully when config is an inline object in angular.json, not a file path (no spurious warning, no crash)',
app: 'examples/jest/simple-app',
command:
'node ../../../packages/jest/tests/validate.js --run-script=test:inline-config --no-cache --expect-suites=2 --expect-tests=4',
},

// CLI passthrough - validated tests
{
Expand Down
9 changes: 6 additions & 3 deletions packages/jest/tests/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
const { execSync } = require('child_process');
const fs = require('fs');

// Parse args: separate jest args from --expect-* args
// Parse args: separate jest args from --expect-* and --run-script=* args
const jestArgs = [];
const expectations = {};
let runScript = 'test';

for (let i = 2; i < process.argv.length; i++) {
const arg = process.argv[i];
if (arg.startsWith('--expect-')) {
const [key, value] = arg.slice(9).split('=');
expectations[key] = value;
} else if (arg.startsWith('--run-script=')) {
runScript = arg.slice('--run-script='.length);
} else {
jestArgs.push(arg);
}
Expand All @@ -19,11 +22,11 @@ for (let i = 2; i < process.argv.length; i++) {
// Quote args that contain spaces
const quotedArgs = jestArgs.map(arg => (arg.includes(' ') ? `"${arg}"` : arg));

console.log(`Running: yarn test ${quotedArgs.join(' ')}`);
console.log(`Running: yarn ${runScript} ${quotedArgs.join(' ')}`);
console.log(`Expectations:`, expectations);

// Capture both stdout and stderr
const output = execSync(`yarn test ${quotedArgs.join(' ')} 2>&1`, {
const output = execSync(`yarn ${runScript} ${quotedArgs.join(' ')} 2>&1`, {
encoding: 'utf-8',
});

Expand Down
Loading