test: test compile dev/prod webpack config

This commit is contained in:
endiliey 2018-08-11 02:50:41 +08:00
parent 80204b5617
commit a4cc782858
15 changed files with 93 additions and 0 deletions

View file

@ -0,0 +1,7 @@
module.exports = {
title: 'Sakura',
tagline: 'This is not an ordinary site',
organizationName: 'endiliey',
projectName: 'sakura',
baseUrl: '/sakura/'
};

View file

@ -0,0 +1,3 @@
import React from 'react';
export default () => <div>Baz</div>;

View file

@ -0,0 +1,3 @@
import React from 'react';
export default () => <div>Foo</div>;

View file

@ -0,0 +1,3 @@
import React from 'react';
export default () => <div>Foo in subfolder</div>;

View file

@ -0,0 +1,3 @@
import React from 'react';
export default () => <div>Index</div>;

View file

@ -0,0 +1,7 @@
module.exports = {
title: 'Hello',
tagline: 'Hello World',
organizationName: 'endiliey',
projectName: 'hello',
baseUrl: '/'
};

19
test/loadSetup.js Normal file
View file

@ -0,0 +1,19 @@
import path from 'path';
import load from '@lib/load';
// Helper methods to setup dummy/ fake projects
const loadSetup = async name => {
const simpleWebsite = path.join(__dirname, '__fixtures__', 'simple-website');
const customWebsite = path.join(__dirname, '__fixtures__', 'custom-website');
switch (name) {
case 'simple':
return await load(simpleWebsite);
case 'custom':
return await load(customWebsite);
default:
return {};
}
};
export default loadSetup;

View file

@ -0,0 +1,48 @@
import webpack from 'webpack';
import path from 'path';
import createBaseConfig from '@lib/webpack/base';
import createDevConfig from '@lib/webpack/dev';
import createProdConfig from '@lib/webpack/prod';
import loadSetup from '../loadSetup';
// webpack compiler helper function
function compile(config) {
return new Promise((resolve, reject) => {
webpack(config, (err, stats) => {
if (err || stats.hasErrors()) {
reject(new Error(`Failed to compile with errors`));
}
resolve('Compiled successfully');
});
});
}
describe('webpack', () => {
test('dev simple', async () => {
console.log = jest.fn();
const props = await loadSetup('simple');
const config = createDevConfig(props).toConfig();
return expect(compile(config)).resolves.toBe('Compiled successfully');
});
test('dev custom', async () => {
console.log = jest.fn();
const props = await loadSetup('custom');
const config = createDevConfig(props).toConfig();
return expect(compile(config)).resolves.toBe('Compiled successfully');
});
test('prod simple', async () => {
console.log = jest.fn();
const props = await loadSetup('simple');
const config = createProdConfig(props).toConfig();
return expect(compile(config)).resolves.toBe('Compiled successfully');
});
test('prod custom', async () => {
console.log = jest.fn();
const props = await loadSetup('custom');
const config = createProdConfig(props).toConfig();
return expect(compile(config)).resolves.toBe('Compiled successfully');
});
});