Skip to content

Commit

Permalink
tests(init): assert for webpack config file content (#2300)
Browse files Browse the repository at this point in the history
* tests: assert for webpack config file content

* fix: update matching condition regex
  • Loading branch information
jamesgeorge007 committed Jan 4, 2021
1 parent 3016370 commit 4ea0cb4
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/generators/__tests__/utils/styleSupport.test.ts
Expand Up @@ -34,7 +34,7 @@ describe('styleSupport', () => {
it('generates SASS configuration', () => {
const gen = getMockGenerator();
const { ExtractUseProps, regExpForStyles } = style(gen, StylingType.SASS);
expect(regExpForStyles).toEqual('/.(scss|css)$/');
expect(regExpForStyles).toEqual('/.(sa|sc|c)ss$/');
expect(gen.dependencies).toEqual(['node-sass', 'sass-loader', 'css-loader', 'style-loader']);
expect(ExtractUseProps.length).toEqual(3);
});
Expand All @@ -43,7 +43,7 @@ describe('styleSupport', () => {
const gen = getMockGenerator();
gen.isProd = true;
const { ExtractUseProps, regExpForStyles } = style(gen, StylingType.SASS);
expect(regExpForStyles).toEqual('/.(scss|css)$/');
expect(regExpForStyles).toEqual('/.(sa|sc|c)ss$/');
expect(gen.dependencies).toEqual(['node-sass', 'sass-loader', 'css-loader']);
expect(ExtractUseProps.length).toEqual(2);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/generators/src/utils/styleSupport.ts
Expand Up @@ -18,7 +18,7 @@ export enum LoaderName {

export enum StyleRegex {
CSS = '/.css$/',
SASS = '/.(scss|css)$/',
SASS = '/.(sa|sc|c)ss$/',
LESS = '/.(less|css)$/',
PostCSS = '/.css$/',
}
Expand Down
6 changes: 5 additions & 1 deletion test/init/auto/init-auto.test.js
Expand Up @@ -35,12 +35,16 @@ describe('init auto flag', () => {
}

// Test regressively files are scaffolded
const files = ['./sw.js', './package.json', './src/index.js'];
const files = ['./sw.js', './package.json', './src/index.js', './webpack.config.js'];

files.forEach((file) => {
expect(fs.existsSync(resolve(genPath, file))).toBeTruthy();
});

const webpackConfig = require(join(genPath, 'webpack.config.js'));

expect(webpackConfig.modules.rules).toEqual([]);

// Check package json is correctly configured
const pkgJsonTests = () => {
const pkgJson = require(join(genPath, './package.json'));
Expand Down
4 changes: 4 additions & 0 deletions test/init/force/init-force.test.js
Expand Up @@ -36,6 +36,10 @@ describe('init force flag', () => {
expect(fs.existsSync(resolve(genPath, file))).toBeTruthy();
});

const webpackConfig = require(join(genPath, 'webpack.config.js'));

expect(webpackConfig.modules.rules).toEqual([]);

// Check package json is correctly configured
const pkgJsonTests = () => {
const pkgJson = require(join(genPath, './package.json'));
Expand Down
8 changes: 8 additions & 0 deletions test/init/generation-path/init-generation-path.test.js
Expand Up @@ -32,6 +32,10 @@ describe('init generate-path flag', () => {
expect(fs.existsSync(resolve(genPath, file))).toBeTruthy();
});

const webpackConfig = require(join(genPath, 'webpack.config.js'));

expect(webpackConfig.modules.rules).toEqual([]);

// Check package json is correctly configured
const pkgJsonTests = () => {
const pkgJson = require(join(genPath, './package.json'));
Expand Down Expand Up @@ -64,6 +68,10 @@ describe('init generate-path flag', () => {
expect(fs.existsSync(resolve(genPath, file))).toBeTruthy();
});

const webpackConfig = require(join(genPath, 'webpack.config.js'));

expect(webpackConfig.modules.rules).toEqual([]);

// Check package json is correctly configured
const pkgJsonTests = () => {
const pkgJson = require(join(genPath, './package.json'));
Expand Down
6 changes: 5 additions & 1 deletion test/init/generator/init-inquirer.test.js
Expand Up @@ -30,12 +30,16 @@ describe('init', () => {
}

// Test regressively files are scaffolded
const files = ['./sw.js', './package.json', './src/index.js'];
const files = ['./sw.js', './package.json', './src/index.js', './webpack.config.js'];

files.forEach((file) => {
expect(fs.existsSync(resolve(genPath, file))).toBeTruthy();
});

const webpackConfig = require(join(genPath, 'webpack.config.js'));

expect(webpackConfig.modules.rules).toEqual([]);

// Check package json is correctly configured
const pkgJsonTests = () => {
const pkgJson = require(join(genPath, './package.json'));
Expand Down
33 changes: 32 additions & 1 deletion test/init/language/css/init-language-css.test.js
Expand Up @@ -32,12 +32,43 @@ describe('init with SCSS', () => {
}

// Test regressively files are scaffolded
const files = ['./package.json', './.yo-rc.json', './src/index.js', 'webpack.config.js'];
const files = ['./package.json', './.yo-rc.json', './src/index.js', './webpack.config.js'];

files.forEach((file) => {
expect(fs.existsSync(resolve(genPath, file))).toBeTruthy();
});

const webpackConfig = require(join(genPath, './webpack.config.js'));

expect(webpackConfig.module.rules).toEqual([
{
test: /.(sa|sc|c)ss$/,

use: [
{
loader: MiniCssExtractPlugin.loader, // eslint-disable-line
},
{
loader: 'style-loader',
},
{
loader: 'css-loader',

options: {
sourceMap: true,
},
},
{
loader: 'sass-loader',

options: {
sourceMap: true,
},
},
],
},
]);

// Check if package.json is correctly configured
const pkgJsonTests = () => {
const pkgJson = require(join(genPath, './package.json'));
Expand Down
12 changes: 12 additions & 0 deletions test/init/language/js/init-language-js.test.js
Expand Up @@ -34,6 +34,18 @@ describe('init with Typescript', () => {
expect(fs.existsSync(resolve(genPath, file))).toBeTruthy();
});

const webpackConfig = require(join(genPath, 'webpack.config.js'));

expect(webpackConfig.module.rules).toEqual([
{
test: /\.(ts|tsx)$/,
loader: 'ts-loader',
include: [path.resolve(__dirname, 'src')], // eslint-disable-line
exclude: [/node_modules/],
},
]);
expect(webpackConfig.resolve.extensions).toEqual(['.tsx', '.ts', '.js']);

// Check package json is correctly configured
const pkgJsonTests = () => {
const pkgJson = require(join(genPath, './package.json'));
Expand Down
29 changes: 24 additions & 5 deletions test/init/multipleEntries/init-multipleEntries.test.js
@@ -1,13 +1,13 @@
'use strict';

const fs = require('fs');
const path = require('path');
const { join, resolve } = require('path');
const rimraf = require('rimraf');
const { runPromptWithAnswers } = require('../../utils/test-utils');
const firstPrompt = 'Will your application have multiple bundles?';

const ENTER = '\x0D';
const genPath = path.join(__dirname, 'test-assets');
const genPath = join(__dirname, 'test-assets');

describe('init with multiple entries', () => {
beforeAll(() => {
Expand All @@ -22,15 +22,34 @@ describe('init with multiple entries', () => {
expect(stdout).toContain(firstPrompt);

// Skip test in case installation fails
if (!fs.existsSync(path.resolve(genPath, './yarn.lock'))) {
if (!fs.existsSync(resolve(genPath, './yarn.lock'))) {
return;
}

// Test regressively files are scaffolded
const files = ['./package.json', './src/a.js', './src/b.js', './.yo-rc.json'];
const files = ['./package.json', './src/a.js', './src/b.js', './.yo-rc.json', './webpack.config.js'];

files.forEach((file) => {
expect(fs.existsSync(path.resolve(genPath, file))).toBeTruthy();
expect(fs.existsSync(resolve(genPath, file))).toBeTruthy();
});

const webpackConfig = require(join(genPath, 'webpack.config.js'));

expect(webpackConfig.entry).toEqual({
a: './src/a.js',
b: './src/b.js',
});
expect(webpackConfig.module.rules).toEqual([]);

// Check if package.json is correctly configured
const pkgJsonTests = () => {
const pkgJson = require(join(genPath, './package.json'));
expect(pkgJson).toBeTruthy();
expect(pkgJson['devDependencies']).toBeTruthy();
expect(pkgJson['devDependencies']['webpack']).toBeTruthy();
expect(pkgJson['devDependencies']['terser-webpack-plugin']).toBeTruthy();
expect(pkgJson['scripts']['build'] == 'webpack').toBeTruthy();
};
expect(pkgJsonTests).not.toThrow();
});
});

0 comments on commit 4ea0cb4

Please sign in to comment.