Skip to content

Commit 30b928e

Browse files
committed
test: Adds test suite for config files
1 parent 03a2dec commit 30b928e

27 files changed

+1514
-1311
lines changed

packages/cli/jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ module.exports = {
3131

3232
// This option allows use of a custom test runner
3333
// testRunner: "jest-circus/runner",
34+
testEnvironment: "node"
3435
};

packages/cli/lib/lib/webpack/transform-config.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,20 @@ module.exports = async function (env, webpackConfig, isServer = false) {
9898
env.config = configFile;
9999
let myConfig = resolve(env.cwd, env.config);
100100

101+
let m;
101102
try {
102-
await stat(myConfig);
103-
} catch (e) {
104-
if (isDefault) return;
105-
throw new Error(
106-
`preact-cli config could not be loaded!\nFile ${env.config} not found.`
107-
);
108-
}
109-
110-
let m = esmImport(myConfig);
111-
112-
// The line above results in an empty object w/ Jest,
113-
// so we need to do the following in order to load it:
114-
if (Object.keys(m).length === 0) {
115-
m = require(myConfig);
103+
m = esmImport(myConfig);
104+
} catch (err) {
105+
const notFound = err.message.includes('Cannot find module');
106+
if (notFound && isDefault) return;
107+
if (notFound) {
108+
throw new Error(
109+
`Failed to load preact-cli config!\nFile ${env.config} not found.\n`
110+
);
111+
}
112+
throw new Error(`Failed to load preact-cli config!\n${
113+
env.verbose ? err.stack : err.message
114+
}\n`);
116115
}
117116

118117
const transformers = parseConfig((m && m.default) || m);

packages/cli/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
"homepage": "https://github.com/preactjs/preact-cli",
3535
"devDependencies": {
3636
"@types/express": "^4.17.13",
37-
"@types/jest": "^27.4.0",
37+
"@types/jest": "^24.9.1",
3838
"html-looks-like": "^1.0.2",
39-
"jest": "^27.0.1",
39+
"jest": "^24.9.0",
4040
"less": "^4.1.1",
4141
"less-loader": "^7.3.0",
4242
"ncp": "^2.0.0",

packages/cli/tests/config.test.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const { build } = require('./lib/cli');
2+
const { subject } = require('./lib/output');
3+
4+
const formats = ['cjs', 'esm'];
5+
6+
const prerenderUrlFiles = [
7+
'array.js',
8+
'stringified-array.js',
9+
'function-returning-array.js',
10+
'function-returning-stringified-array.js',
11+
];
12+
13+
const preactConfigFiles = ['function.js', 'object.js'];
14+
15+
describe('config files', () => {
16+
describe('prerender-urls', () => {
17+
it(`should load the 'prerender-urls.json' file`, async () => {
18+
let dir = await subject('multiple-config-files');
19+
20+
const logSpy = jest.spyOn(process.stdout, 'write');
21+
22+
await build(dir);
23+
24+
expect(logSpy).not.toHaveBeenCalledWith(
25+
expect.stringContaining(
26+
'Failed to load prerenderUrls file, using default!'
27+
)
28+
);
29+
});
30+
31+
formats.forEach(moduleFormat => {
32+
prerenderUrlFiles.forEach(dataFormat => {
33+
it(`should load the '${dataFormat}' file in ${moduleFormat}`, async () => {
34+
let dir = await subject('multiple-config-files');
35+
36+
const logSpy = jest.spyOn(process.stdout, 'write');
37+
38+
await build(dir, {
39+
prerenderUrls: `prerender/${moduleFormat}/${dataFormat}`,
40+
});
41+
42+
expect(logSpy).not.toHaveBeenCalledWith(
43+
expect.stringContaining(
44+
'Failed to load prerenderUrls file, using default!'
45+
)
46+
);
47+
});
48+
});
49+
50+
it(`should fail to load malformed prerender-urls data in ${moduleFormat}`, async () => {
51+
let dir = await subject('multiple-config-files');
52+
53+
const logSpy = jest.spyOn(process.stdout, 'write');
54+
55+
await build(dir, {
56+
prerenderUrls: `prerender/${moduleFormat}/returns-bad-json.js`,
57+
});
58+
59+
expect(logSpy).toHaveBeenCalledWith(
60+
expect.stringContaining(
61+
'Failed to load prerenderUrls file, using default!'
62+
)
63+
);
64+
});
65+
});
66+
});
67+
68+
describe('preact.config', () => {
69+
formats.forEach(moduleFormat => {
70+
preactConfigFiles.forEach(dataFormat => {
71+
it(`should load the '${dataFormat}' file in ${moduleFormat}`, async () => {
72+
let dir = await subject('multiple-config-files');
73+
74+
await expect(
75+
build(dir, { config: `preactConfig/${moduleFormat}/${dataFormat}` })
76+
).resolves.not.toThrow();
77+
});
78+
});
79+
80+
it(`should fail to load malformed config data in ${moduleFormat}`, async () => {
81+
let dir = await subject('multiple-config-files');
82+
83+
await expect(
84+
build(dir, {
85+
config: `preactConfig/${moduleFormat}/returns-bad-config.js`,
86+
})
87+
).rejects.toThrow('Failed to load preact-cli config!');
88+
});
89+
});
90+
});
91+
});

packages/cli/tests/setup.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
jest.setTimeout(240 * 1000);
1+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 240 * 1000;
22

33
if (!process.env.LISTENING_TO_UNHANDLED_REJECTION) {
44
process.on('unhandledRejection', err => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { h, Component } from 'preact';
2+
import { Router } from 'preact-router';
3+
import Home from './routes/home';
4+
5+
export default class App extends Component {
6+
handleRoute = e => {
7+
this.currentUrl = e.url;
8+
};
9+
10+
render(props) {
11+
return (
12+
<div id="app">
13+
<Router url={props.url} onChange={this.handleRoute} {...props}>
14+
<Home path="/" />
15+
</Router>
16+
</div>
17+
);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"private": true,
3+
"name": "preact-config"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function (config, env, helpers) {
2+
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
webpack(config, env, helpers) {}
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module.exports = {
2+
webpack(config, env, helpers) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function (config, env, helpers) {
2+
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
webpack (config, env, helpers) {}
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default {
2+
webpack (config, env, helpers) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"url": "/"
4+
},
5+
{
6+
"url": "/custom"
7+
}
8+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = [
2+
{
3+
"url": "/"
4+
},
5+
{
6+
"url": "/custom"
7+
}
8+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = () => [
2+
{
3+
"url": "/"
4+
},
5+
{
6+
"url": "/custom"
7+
}
8+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = () => `[
2+
{
3+
"url": "/"
4+
},
5+
{
6+
"url": "/custom"
7+
}
8+
]`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = () => `[
2+
{
3+
"url": "/"
4+
},
5+
{
6+
"url": "/custom"
7+
}
8+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = `[
2+
{
3+
"url": "/"
4+
},
5+
{
6+
"url": "/custom"
7+
}
8+
]`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default [
2+
{
3+
"url": "/"
4+
},
5+
{
6+
"url": "/custom"
7+
}
8+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default () => [
2+
{
3+
"url": "/"
4+
},
5+
{
6+
"url": "/custom"
7+
}
8+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default () => `[
2+
{
3+
"url": "/"
4+
},
5+
{
6+
"url": "/custom"
7+
}
8+
]`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default () => `[
2+
{
3+
"url": "/"
4+
},
5+
{
6+
"url": "/custom"
7+
}
8+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default `[
2+
{
3+
"url": "/"
4+
},
5+
{
6+
"url": "/custom"
7+
}
8+
]`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.home {
2+
background: red;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './home.css';
2+
3+
export default () => <div>Home</div>;

0 commit comments

Comments
 (0)