/
next-gen.js
110 lines (85 loc) 路 2.96 KB
/
next-gen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from '@ava/test';
import sinon from 'sinon';
import {loadConfig} from '../../lib/load-config.js';
const FIXTURE_ROOT = fileURLToPath(new URL('fixtures', import.meta.url));
const resolve = relpath => path.resolve(FIXTURE_ROOT, relpath);
const loadFromSetup = async (setup, t, assertUnsupportedFiles = (tt, files) => tt.is(files.length, 0)) => {
if (typeof setup === 'string') {
const loaded = await loadConfig();
return loaded.config;
}
const {
configFile,
defaults,
resolveFrom,
} = setup;
const loaded = await loadConfig({configFile, defaults, resolveFrom});
assertUnsupportedFiles(t, loaded.unsupportedFiles);
return loaded.config;
};
const ok = setup => async (t, assert = tt => tt.pass(), assertUnsupportedFiles = undefined) => {
const fixture = typeof setup === 'string' ? setup : setup.fixture;
const stub = sinon.stub(process, 'cwd');
t.teardown(() => stub.restore());
stub.returns(resolve(fixture));
const conf = loadFromSetup(setup, t, assertUnsupportedFiles);
await t.notThrowsAsync(conf);
const result = await t.try(assert, await conf, setup);
result.commit();
};
const notOk = setup => async (t, assert = (tt, error) => tt.snapshot(error.message, 'error message')) => {
const fixture = typeof setup === 'string' ? setup : setup.fixture;
const stub = sinon.stub(process, 'cwd');
t.teardown(() => stub.restore());
stub.returns(resolve(fixture));
const conf = loadFromSetup(setup, t);
const error = await t.throwsAsync(conf);
const result = await t.try(assert, error, setup);
result.commit();
};
test.serial('loads .mjs config', ok('mjs'), (t, conf) => {
t.true(conf.failFast);
});
test.serial('handles errors when loading .mjs config', notOk({
fixture: 'mjs',
configFile: 'error.mjs',
}));
test.serial('fails when .mjs config does not have a default export', notOk({
fixture: 'mjs',
configFile: 'no-default-export.mjs',
}));
test.serial('loads .js config as CommonJS', ok('js-as-cjs'), (t, conf) => {
t.true(conf.failFast);
});
test.serial('loads .js config as ESM', ok('js-as-esm'), (t, conf) => {
t.true(conf.failFast);
});
test.serial('finds unsupported configs',
ok({
fixture: 'unsupported-configs',
}),
(t, conf) => {
t.true(conf.failFast);
},
(t, unsupportedFiles) => {
t.is(unsupportedFiles.length, 1);
t.regex(unsupportedFiles[0], /ava\.config\.json/);
},
);
test.serial('handles errors when loading .js config as ESM', notOk({
fixture: 'js-as-esm',
configFile: 'error.js',
}));
test.serial('fails when .js config does not have a default export', notOk({
fixture: 'js-as-esm',
configFile: 'no-default-export.js',
}));
test.serial('throws an error if .js, .cjs and .mjs configs are present', notOk('file-yes-cjs-yes-mjs-yes'));
test.serial('config factory returns a promise', ok('factory-promise-return'), (t, conf) => {
t.true(conf.failFast);
});
test.serial('config exports a promise', ok('promise-config'), (t, conf) => {
t.true(conf.failFast);
});