Skip to content

Commit

Permalink
test: Adds test suite for config files
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Feb 25, 2022
1 parent 03a2dec commit 7e8c7df
Show file tree
Hide file tree
Showing 27 changed files with 1,507 additions and 1,311 deletions.
1 change: 1 addition & 0 deletions packages/cli/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ module.exports = {

// This option allows use of a custom test runner
// testRunner: "jest-circus/runner",
testEnvironment: "node"
};
27 changes: 13 additions & 14 deletions packages/cli/lib/lib/webpack/transform-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,20 @@ module.exports = async function (env, webpackConfig, isServer = false) {
env.config = configFile;
let myConfig = resolve(env.cwd, env.config);

let m;
try {
await stat(myConfig);
} catch (e) {
if (isDefault) return;
throw new Error(
`preact-cli config could not be loaded!\nFile ${env.config} not found.`
);
}

let m = esmImport(myConfig);

// The line above results in an empty object w/ Jest,
// so we need to do the following in order to load it:
if (Object.keys(m).length === 0) {
m = require(myConfig);
m = esmImport(myConfig);
} catch (err) {
const notFound = err.message.includes('Cannot find module');
if (notFound && isDefault) return;
if (notFound) {
throw new Error(
`Failed to load preact-cli config!\nFile ${env.config} not found.\n`
);
}
throw new Error(`Failed to load preact-cli config!\n${
env.verbose ? err.stack : err.message
}\n`);
}

const transformers = parseConfig((m && m.default) || m);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
"homepage": "https://github.com/preactjs/preact-cli",
"devDependencies": {
"@types/express": "^4.17.13",
"@types/jest": "^27.4.0",
"@types/jest": "^24.9.1",
"html-looks-like": "^1.0.2",
"jest": "^27.0.1",
"jest": "^24.9.0",
"less": "^4.1.1",
"less-loader": "^7.3.0",
"ncp": "^2.0.0",
Expand Down
84 changes: 84 additions & 0 deletions packages/cli/tests/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const { build } = require('./lib/cli');
const { subject } = require('./lib/output');

const formats = ['cjs', 'esm'];

const prerenderUrlFiles = [
'array.js',
'stringified-array.js',
'function-returning-array.js',
'function-returning-stringified-array.js',
];

const preactConfigFiles = [
'function.js',
'object.js'
];

describe('config files', () => {
describe('prerender-urls', () => {
it(`should load the 'prerender-urls.json' file`, async () => {
let dir = await subject('multiple-config-files');

const logSpy = jest.spyOn(process.stdout, 'write');

await build(dir);

expect(logSpy).not.toHaveBeenCalledWith(
expect.stringContaining('Failed to load prerenderUrls file, using default!')
);
});

formats.forEach(moduleFormat => {
prerenderUrlFiles.forEach(dataFormat => {
it(`should load the '${dataFormat}' file in ${moduleFormat}`, async () => {
let dir = await subject('multiple-config-files');

const logSpy = jest.spyOn(process.stdout, 'write');

await build(dir, { prerenderUrls: `prerender/${moduleFormat}/${dataFormat}` });

expect(logSpy).not.toHaveBeenCalledWith(
expect.stringContaining('Failed to load prerenderUrls file, using default!')
);
});
});
});

formats.forEach(moduleFormat => {
it(`should fail to load malformed prerender-urls data in ${moduleFormat}`, async () => {
let dir = await subject('multiple-config-files');

const logSpy = jest.spyOn(process.stdout, 'write');

await build(dir, { prerenderUrls: `prerender/${moduleFormat}/returns-bad-json.js` });

expect(logSpy).toHaveBeenCalledWith(
expect.stringContaining('Failed to load prerenderUrls file, using default!')
);
});
});
});

describe('preact.config', () => {
formats.forEach(moduleFormat => {
preactConfigFiles.forEach(dataFormat => {
it(`should load the '${dataFormat}' file in ${moduleFormat}`, async () => {
let dir = await subject('multiple-config-files');

await expect(
build(dir, { config: `preactConfig/${moduleFormat}/${dataFormat}` })
).resolves.not.toThrow();
});
});

it(`should fail to load malformed config data in ${moduleFormat}`, async () => {
let dir = await subject('multiple-config-files');

await expect(build(dir, { config: `preactConfig/${moduleFormat}/returns-bad-config.js` }))
.rejects
.toThrow('Failed to load preact-cli config!');
});
});
});
});
2 changes: 1 addition & 1 deletion packages/cli/tests/setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jest.setTimeout(240 * 1000);
jasmine.DEFAULT_TIMEOUT_INTERVAL = 240 * 1000;

if (!process.env.LISTENING_TO_UNHANDLED_REJECTION) {
process.on('unhandledRejection', err => {
Expand Down
19 changes: 19 additions & 0 deletions packages/cli/tests/subjects/multiple-config-files/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { h, Component } from 'preact';
import { Router } from 'preact-router';
import Home from './routes/home';

export default class App extends Component {
handleRoute = e => {
this.currentUrl = e.url;
};

render(props) {
return (
<div id="app">
<Router url={props.url} onChange={this.handleRoute} {...props}>
<Home path="/" />
</Router>
</div>
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"name": "preact-config"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (config, env, helpers) {

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
webpack(config, env, helpers) {}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = {
webpack(config, env, helpers) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function (config, env, helpers) {

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
webpack (config, env, helpers) {}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default {
webpack (config, env, helpers) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"url": "/"
},
{
"url": "/custom"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = [
{
"url": "/"
},
{
"url": "/custom"
}
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = () => [
{
"url": "/"
},
{
"url": "/custom"
}
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = () => `[
{
"url": "/"
},
{
"url": "/custom"
}
]`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = () => `[
{
"url": "/"
},
{
"url": "/custom"
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = `[
{
"url": "/"
},
{
"url": "/custom"
}
]`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default [
{
"url": "/"
},
{
"url": "/custom"
}
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default () => [
{
"url": "/"
},
{
"url": "/custom"
}
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default () => `[
{
"url": "/"
},
{
"url": "/custom"
}
]`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default () => `[
{
"url": "/"
},
{
"url": "/custom"
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default `[
{
"url": "/"
},
{
"url": "/custom"
}
]`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.home {
background: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './home.css';

export default () => <div>Home</div>;

0 comments on commit 7e8c7df

Please sign in to comment.