57 lines
3.3 KiB
JavaScript
57 lines
3.3 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { spawnSync } from 'node:child_process';
|
|
import test from 'node:test';
|
|
|
|
const root = mkdtempSync(path.join(os.tmpdir(), 'jobjakt glob callers '));
|
|
const jest = path.resolve(import.meta.dirname, '../node_modules/jest/bin/jest.js');
|
|
const write = (file, content) => {
|
|
const target = path.join(root, file);
|
|
mkdirSync(path.dirname(target), { recursive: true });
|
|
writeFileSync(target, content);
|
|
};
|
|
const run = (config, ...args) => {
|
|
const configFile = path.join(root, 'jest.config.cjs');
|
|
writeFileSync(configFile, `module.exports = ${JSON.stringify({ rootDir: root, testEnvironment: 'node', transform: {}, ...config })};\n`);
|
|
return spawnSync(process.execPath, [jest, '--runInBand', '--config', configFile, ...args], { cwd: root, encoding: 'utf8', timeout: 30000 });
|
|
};
|
|
|
|
test.after(() => rmSync(root, { recursive: true, force: true }));
|
|
|
|
test('Jest expands project globs and discovers both configured suites', () => {
|
|
for (const name of ['one', 'two']) {
|
|
write(`projects/${name}/package.json`, JSON.stringify({ jest: { rootDir: path.join(root, 'projects', name), testEnvironment: 'node', transform: {}, testMatch: ['**/*.test.cjs'] } }));
|
|
write(`projects/${name}/example.test.cjs`, "test('ordinary assertion',()=>expect(1+1).toBe(2));\n");
|
|
}
|
|
const config = { projects: [path.join(root, 'projects/*')] };
|
|
const discovery = run(config, '--listTests', '--json');
|
|
assert.equal(discovery.status, 0, discovery.stderr);
|
|
const names = JSON.parse(discovery.stdout).map(file => path.relative(root, file).replaceAll('\\', '/')).sort();
|
|
assert.deepEqual(names, ['projects/one/example.test.cjs', 'projects/two/example.test.cjs']);
|
|
const execution = run(config, '--json');
|
|
assert.equal(execution.status, 0, execution.stderr);
|
|
assert.equal(JSON.parse(execution.stdout).numPassedTests, 2);
|
|
});
|
|
|
|
test('Jest enforces coverage thresholds selected by a glob', () => {
|
|
write('coverage-source/calculator.js', 'module.exports = value => value + 1;\n');
|
|
write('coverage-test/example.test.cjs', "test('calculator',()=>expect(require('../coverage-source/calculator.js')(1)).toBe(2));\n");
|
|
const config = { testMatch: ['**/coverage-test/*.test.cjs'], collectCoverage: true, coverageProvider: 'v8', coverageReporters: ['json-summary'], collectCoverageFrom: ['coverage-source/*.js'] };
|
|
const positive = run({ ...config, coverageThreshold: { './coverage-source/*.js': { lines: 100 } } });
|
|
assert.equal(positive.status, 0, positive.stderr);
|
|
const negative = run({ ...config, coverageThreshold: { './coverage-source/*.js': { lines: 101 } } });
|
|
assert.equal(negative.status, 1, negative.stderr);
|
|
assert.match(negative.stderr, /Coverage for lines \(100%\) does not meet .* threshold \(101%\)/);
|
|
});
|
|
|
|
test('Jest missing-import diagnostics retain sibling extension suggestions', () => {
|
|
write('missing-import/module.custom', 'synthetic fixture\n');
|
|
write('missing-import/example.test.cjs', "test('missing extension',()=>require('./module'));\n");
|
|
const result = run({ testMatch: ['**/missing-import/*.test.cjs'] });
|
|
assert.equal(result.status, 1, result.stderr);
|
|
assert.match(result.stderr, /module\.custom/);
|
|
assert.match(result.stderr, /You might want to include a file extension/);
|
|
});
|