Skip to content

Commit

Permalink
test: importLoaders option (#841)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Dec 3, 2018
1 parent 1dad1fb commit ee2d253
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 0 deletions.
196 changes: 196 additions & 0 deletions test/__snapshots__/importLoaders-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`importLoaders option 0 (\`postcss-loader\` before): errors 1`] = `Array []`;

exports[`importLoaders option 0 (\`postcss-loader\` before): module (evaluated) 1`] = `
Array [
Array [
2,
".bar {
color: blue;
color: rgb(0 0 100% / 90%);
}
",
"",
],
Array [
1,
".foo {
color: red;
color: rgba(0, 0, 255, 0.9);
}
",
"",
],
]
`;

exports[`importLoaders option 0 (\`postcss-loader\` before): module 1`] = `
"exports = module.exports = require(\\"../../../lib/runtime/api.js\\")(false);
// imports
exports.i(require(\\"-!../../../index.js??ref--4-0!./imported.css\\"), \\"\\");
// module
exports.push([module.id, \\".foo {\\\\n color: red;\\\\n color: rgba(0, 0, 255, 0.9);\\\\n}\\\\n\\", \\"\\"]);
// exports
"
`;

exports[`importLoaders option 0 (\`postcss-loader\` before): warnings 1`] = `Array []`;

exports[`importLoaders option 1 (\`postcss-loader\` before): errors 1`] = `Array []`;

exports[`importLoaders option 1 (\`postcss-loader\` before): module (evaluated) 1`] = `
Array [
Array [
2,
".bar {
color: blue;
color: rgba(0, 0, 255, 0.9);
}
",
"",
],
Array [
1,
".foo {
color: red;
color: rgba(0, 0, 255, 0.9);
}
",
"",
],
]
`;

exports[`importLoaders option 1 (\`postcss-loader\` before): module 1`] = `
"exports = module.exports = require(\\"../../../lib/runtime/api.js\\")(false);
// imports
exports.i(require(\\"-!../../../index.js??ref--4-0!../../../node_modules/postcss-loader/src/index.js??ref--4-1!./imported.css\\"), \\"\\");
// module
exports.push([module.id, \\".foo {\\\\n color: red;\\\\n color: rgba(0, 0, 255, 0.9);\\\\n}\\\\n\\", \\"\\"]);
// exports
"
`;

exports[`importLoaders option 1 (\`postcss-loader\` before): warnings 1`] = `Array []`;

exports[`importLoaders option 1 (no loaders before): errors 1`] = `Array []`;

exports[`importLoaders option 1 (no loaders before): module (evaluated) 1`] = `
Array [
Array [
2,
".bar {
color: blue;
color: rgb(0 0 100% / 90%);
}
",
"",
],
Array [
1,
".foo {
color: red;
color: rgb(0 0 100% / 90%);
}
",
"",
],
]
`;

exports[`importLoaders option 1 (no loaders before): module 1`] = `
"exports = module.exports = require(\\"../../../lib/runtime/api.js\\")(false);
// imports
exports.i(require(\\"-!../../../index.js??ref--4-0!./imported.css\\"), \\"\\");
// module
exports.push([module.id, \\".foo {\\\\n color: red;\\\\n color: rgb(0 0 100% / 90%);\\\\n}\\\\n\\", \\"\\"]);
// exports
"
`;

exports[`importLoaders option 1 (no loaders before): warnings 1`] = `Array []`;

exports[`importLoaders option 2 (\`postcss-loader\` before): errors 1`] = `Array []`;

exports[`importLoaders option 2 (\`postcss-loader\` before): module (evaluated) 1`] = `
Array [
Array [
2,
".bar {
color: blue;
color: rgba(0, 0, 255, 0.9);
}
",
"",
],
Array [
1,
".foo {
color: red;
color: rgba(0, 0, 255, 0.9);
}
",
"",
],
]
`;

exports[`importLoaders option 2 (\`postcss-loader\` before): module 1`] = `
"exports = module.exports = require(\\"../../../lib/runtime/api.js\\")(false);
// imports
exports.i(require(\\"-!../../../index.js??ref--4-0!../../../node_modules/postcss-loader/src/index.js??ref--4-1!./imported.css\\"), \\"\\");
// module
exports.push([module.id, \\".foo {\\\\n color: red;\\\\n color: rgba(0, 0, 255, 0.9);\\\\n}\\\\n\\", \\"\\"]);
// exports
"
`;

exports[`importLoaders option 2 (\`postcss-loader\` before): warnings 1`] = `Array []`;

exports[`importLoaders option not specify (no loader before): errors 1`] = `Array []`;

exports[`importLoaders option not specify (no loader before): module (evaluated) 1`] = `
Array [
Array [
2,
".bar {
color: blue;
color: rgb(0 0 100% / 90%);
}
",
"",
],
Array [
1,
".foo {
color: red;
color: rgba(0, 0, 255, 0.9);
}
",
"",
],
]
`;

exports[`importLoaders option not specify (no loader before): module 1`] = `
"exports = module.exports = require(\\"../../../lib/runtime/api.js\\")(false);
// imports
exports.i(require(\\"-!../../../index.js??ref--4-0!./imported.css\\"), \\"\\");
// module
exports.push([module.id, \\".foo {\\\\n color: red;\\\\n color: rgba(0, 0, 255, 0.9);\\\\n}\\\\n\\", \\"\\"]);
// exports
"
`;

exports[`importLoaders option not specify (no loader before): warnings 1`] = `Array []`;
4 changes: 4 additions & 0 deletions test/fixtures/nested-import/imported.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.bar {
color: blue;
color: rgb(0 0 100% / 90%);
}
6 changes: 6 additions & 0 deletions test/fixtures/nested-import/source.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import './imported.css';

.foo {
color: red;
color: rgb(0 0 100% / 90%);
}
1 change: 1 addition & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function evaluated(output, modules, moduleId = 1) {
const importedModule = modules.find((el) => {
const modulePath = el.identifier.split('!').pop();
const importedPaths = [
'nested-import',
'postcss-present-env',
'icss/tests-cases/import',
'icss/tests-cases/import-reserved-keywords',
Expand Down
106 changes: 106 additions & 0 deletions test/importLoaders-option.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const postcssPresetEnv = require('postcss-preset-env');

const { webpack, evaluated } = require('./helpers');

describe('importLoaders option', () => {
it('not specify (no loader before)', async () => {
// It is hard to test `postcss` on reuse `ast`, please look on coverage before merging
const config = {
postcssLoader: true,
postcssLoaderOptions: {
plugins: () => [postcssPresetEnv({ stage: 0 })],
},
};
const testId = './nested-import/source.css';
const stats = await webpack(testId, config);
const { modules } = stats.toJson();
const module = modules.find((m) => m.id === testId);

expect(module.source).toMatchSnapshot('module');
expect(evaluated(module.source, modules)).toMatchSnapshot(
'module (evaluated)'
);
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it('1 (no loaders before)', async () => {
const config = {
loader: { options: { importLoaders: 1 } },
};
const testId = './nested-import/source.css';
const stats = await webpack(testId, config);
const { modules } = stats.toJson();
const module = modules.find((m) => m.id === testId);

expect(module.source).toMatchSnapshot('module');
expect(evaluated(module.source, modules)).toMatchSnapshot(
'module (evaluated)'
);
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it('0 (`postcss-loader` before)', async () => {
const config = {
loader: { options: { importLoaders: 0 } },
postcssLoader: true,
postcssLoaderOptions: {
plugins: () => [postcssPresetEnv({ stage: 0 })],
},
};
const testId = './nested-import/source.css';
const stats = await webpack(testId, config);
const { modules } = stats.toJson();
const module = modules.find((m) => m.id === testId);

expect(module.source).toMatchSnapshot('module');
expect(evaluated(module.source, modules)).toMatchSnapshot(
'module (evaluated)'
);
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it('1 (`postcss-loader` before)', async () => {
const config = {
loader: { options: { importLoaders: 1 } },
postcssLoader: true,
postcssLoaderOptions: {
plugins: () => [postcssPresetEnv({ stage: 0 })],
},
};
const testId = './nested-import/source.css';
const stats = await webpack(testId, config);
const { modules } = stats.toJson();
const module = modules.find((m) => m.id === testId);

expect(module.source).toMatchSnapshot('module');
expect(evaluated(module.source, modules)).toMatchSnapshot(
'module (evaluated)'
);
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});

it('2 (`postcss-loader` before)', async () => {
const config = {
loader: { options: { importLoaders: 2 } },
postcssLoader: true,
postcssLoaderOptions: {
plugins: () => [postcssPresetEnv({ stage: 0 })],
},
};
const testId = './nested-import/source.css';
const stats = await webpack(testId, config);
const { modules } = stats.toJson();
const module = modules.find((m) => m.id === testId);

expect(module.source).toMatchSnapshot('module');
expect(evaluated(module.source, modules)).toMatchSnapshot(
'module (evaluated)'
);
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
expect(stats.compilation.errors).toMatchSnapshot('errors');
});
});

0 comments on commit ee2d253

Please sign in to comment.