Skip to content

Commit

Permalink
test: refactor (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed May 7, 2020
1 parent a728675 commit 1e2f3a1
Show file tree
Hide file tree
Showing 23 changed files with 270 additions and 203 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/nodejs.yml
Expand Up @@ -57,6 +57,13 @@ jobs:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [10.x, 12.x, 14.x]
webpack-version: [latest, next]
exclude:
- os: macos-latest
node-version: 12.x
webpack-version: latest
- os: macos-latest
node-version: 14.x
webpack-version: latest

runs-on: ${{ matrix.os }}

Expand Down
1 change: 1 addition & 0 deletions jest.config.js
@@ -1,3 +1,4 @@
module.exports = {
testEnvironment: 'node',
globalSetup: '<rootDir>/globalSetup.js',
};
106 changes: 61 additions & 45 deletions test/CopyPlugin.test.js
Expand Up @@ -2,6 +2,8 @@ import path from 'path';

import { run, runEmit, runChange } from './helpers/run';

import { readAssets } from './helpers';

const FIXTURES_DIR = path.join(__dirname, 'fixtures');

describe('apply function', () => {
Expand Down Expand Up @@ -309,19 +311,19 @@ describe('apply function', () => {

describe('watch mode', () => {
it('should add the file to the watch list when "from" is a file', (done) => {
const expectedAssetKeys = ['file.txt'];

run({
patterns: [
{
from: 'file.txt',
},
],
})
.then((compilation) => {
const absFrom = path.join(FIXTURES_DIR, 'file.txt');

expect(Array.from(compilation.fileDependencies).sort()).toEqual(
[absFrom].sort()
);
.then(({ compiler, stats }) => {
expect(
Array.from(Object.keys(readAssets(compiler, stats))).sort()
).toEqual(expectedAssetKeys);
})
.then(done)
.catch(done);
Expand All @@ -335,12 +337,13 @@ describe('apply function', () => {
},
],
})
.then((compilation) => {
const absFrom = path.join(FIXTURES_DIR, 'directory');

expect(Array.from(compilation.contextDependencies).sort()).toEqual(
[absFrom].sort()
.then(({ stats }) => {
const { contextDependencies } = stats.compilation;
const isIncludeDependency = contextDependencies.has(
path.join(FIXTURES_DIR, 'directory')
);

expect(isIncludeDependency).toBe(true);
})
.then(done)
.catch(done);
Expand All @@ -354,29 +357,32 @@ describe('apply function', () => {
},
],
})
.then((compilation) => {
expect(
Array.from(compilation.contextDependencies)
.map((contextDependency) => contextDependency)
.sort()
).toEqual([path.join(FIXTURES_DIR, 'directory')].sort());
.then(({ stats }) => {
const { contextDependencies } = stats.compilation;
const isIncludeDependency = contextDependencies.has(
path.join(FIXTURES_DIR, 'directory')
);

expect(isIncludeDependency).toBe(true);
})
.then(done)
.catch(done);
});

it('should not add the directory to the watch list when glob is a file', (done) => {
const expectedAssetKeys = ['directoryfile.txt'];

run({
patterns: [
{
from: 'directory/directoryfile.txt',
},
],
})
.then((compilation) => {
const absFrom = path.join(FIXTURES_DIR, 'directory');

expect(compilation.contextDependencies).not.toContain(absFrom);
.then(({ compiler, stats }) => {
expect(Array.from(Object.keys(readAssets(compiler, stats)))).toEqual(
expectedAssetKeys
);
})
.then(done)
.catch(done);
Expand All @@ -385,16 +391,16 @@ describe('apply function', () => {
it('should include files that have changed when `from` is a file', (done) => {
runChange({
expectedAssetKeys: ['tempfile1.txt', 'tempfile2.txt'],
newFileLoc1: path.join(FIXTURES_DIR, 'watch', 'tempfile1.txt'),
newFileLoc2: path.join(FIXTURES_DIR, 'watch', 'tempfile2.txt'),
newFileLoc1: path.join(FIXTURES_DIR, 'watch', '_t5', 'tempfile1.txt'),
newFileLoc2: path.join(FIXTURES_DIR, 'watch', '_t5', 'tempfile2.txt'),
patterns: [
{
from: 'tempfile1.txt',
context: 'watch',
context: 'watch/_t5',
},
{
from: 'tempfile2.txt',
context: 'watch',
context: 'watch/_t5',
},
],
})
Expand All @@ -408,18 +414,20 @@ describe('apply function', () => {
newFileLoc1: path.join(
FIXTURES_DIR,
'watch',
'_t4',
'directory',
'tempfile1.txt'
),
newFileLoc2: path.join(
FIXTURES_DIR,
'watch',
'_t4',
'directory',
'tempfile2.txt'
),
patterns: [
{
from: 'watch/directory',
from: 'watch/_t4/directory',
},
],
})
Expand All @@ -429,24 +437,29 @@ describe('apply function', () => {

it('should include all files when `from` is a glob', (done) => {
runChange({
expectedAssetKeys: ['dest1/tempfile1.txt', 'dest1/tempfile2.txt'],
expectedAssetKeys: [
'_t3/dest1/tempfile1.txt',
'_t3/dest1/tempfile2.txt',
],
newFileLoc1: path.join(
FIXTURES_DIR,
'watch',
'_t3',
'directory',
'tempfile1.txt'
),
newFileLoc2: path.join(
FIXTURES_DIR,
'watch',
'_t3',
'directory',
'tempfile2.txt'
),
patterns: [
{
context: 'watch/directory',
context: 'watch/_t3/directory',
from: '**/*.txt',
to: 'dest1',
to: '_t3/dest1',
},
],
})
Expand All @@ -457,33 +470,35 @@ describe('apply function', () => {
it('should include all files when multiple patterns used', (done) => {
runChange({
expectedAssetKeys: [
'dest1/tempfile1.txt',
'dest1/tempfile2.txt',
'dest2/tempfile1.txt',
'dest2/tempfile2.txt',
'_t2/dest1/tempfile1.txt',
'_t2/dest1/tempfile2.txt',
'_t2/dest2/tempfile1.txt',
'_t2/dest2/tempfile2.txt',
],
newFileLoc1: path.join(
FIXTURES_DIR,
'watch',
'_t2',
'directory',
'tempfile1.txt'
),
newFileLoc2: path.join(
FIXTURES_DIR,
'watch',
'_t2',
'directory',
'tempfile2.txt'
),
patterns: [
{
context: 'watch/directory',
context: 'watch/_t2/directory',
from: '**/*.txt',
to: 'dest1',
to: '_t2/dest1',
},
{
context: 'watch/directory',
context: 'watch/_t2/directory',
from: '**/*.txt',
to: 'dest2',
to: '_t2/dest2',
},
],
})
Expand All @@ -494,27 +509,28 @@ describe('apply function', () => {
it('should include all files when multiple patterns with difference contexts', (done) => {
runChange({
expectedAssetKeys: [
'dest1/tempfile1.txt',
'dest2/directory/tempfile1.txt',
'dest2/tempfile2.txt',
'_t1/dest1/tempfile1.txt',
'_t1/dest2/directory/tempfile1.txt',
'_t1/dest2/tempfile2.txt',
],
newFileLoc1: path.join(
FIXTURES_DIR,
'watch',
'_t1',
'directory',
'tempfile1.txt'
),
newFileLoc2: path.join(FIXTURES_DIR, 'watch', 'tempfile2.txt'),
newFileLoc2: path.join(FIXTURES_DIR, 'watch', '_t1', 'tempfile2.txt'),
patterns: [
{
context: 'watch/directory',
context: 'watch/_t1/directory',
from: '**/*.txt',
to: 'dest1',
to: '_t1/dest1',
},
{
context: 'watch',
context: 'watch/_t1',
from: '**/*.txt',
to: 'dest2',
to: '_t1/dest2',
},
],
})
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
24 changes: 24 additions & 0 deletions test/helpers/PreCopyPlugin.js
@@ -0,0 +1,24 @@
class PreCopyPlugin {
constructor(options = {}) {
this.options = options.options || {};
}

apply(compiler) {
const plugin = { name: 'PreCopyPlugin' };

compiler.hooks.emit.tapAsync(plugin, (compilation, callback) => {
this.options.existingAssets.forEach((assetName) => {
// eslint-disable-next-line no-param-reassign
compilation.assets[assetName] = {
source() {
return 'existing';
},
};
});

callback();
});
}
}

export default PreCopyPlugin;
11 changes: 11 additions & 0 deletions test/helpers/compile.js
@@ -0,0 +1,11 @@
export default (compiler) => {
return new Promise((resolve, reject) => {
compiler.run((error, stats) => {
if (error) {
return reject(error);
}

return resolve({ stats, compiler });
});
});
};
1 change: 1 addition & 0 deletions test/helpers/enter.js
@@ -0,0 +1 @@
// Entry point for tests
32 changes: 32 additions & 0 deletions test/helpers/getCompiler.js
@@ -0,0 +1,32 @@
import path from 'path';

import webpack from 'webpack';
import { createFsFromVolume, Volume } from 'memfs';

export default (config = {}) => {
const fullConfig = {
mode: 'development',
context: path.resolve(__dirname, '../fixtures'),
entry: path.resolve(__dirname, '../helpers/enter.js'),
output: {
path: path.resolve(__dirname, '../build'),
},
...config,
};

if (webpack.version[0] === 5) {
fullConfig.stats.source = true;
}

const compiler = webpack(fullConfig);

if (!config.outputFileSystem) {
const outputFileSystem = createFsFromVolume(new Volume());
// Todo remove when we drop webpack@4 support
outputFileSystem.join = path.join.bind(path);

compiler.outputFileSystem = outputFileSystem;
}

return compiler;
};
6 changes: 6 additions & 0 deletions test/helpers/index.js
@@ -0,0 +1,6 @@
import compile from './compile';
import getCompiler from './getCompiler';
import readAsset from './readAsset';
import readAssets from './readAssets';

export { compile, getCompiler, readAsset, readAssets };

0 comments on commit 1e2f3a1

Please sign in to comment.