Skip to content

Commit

Permalink
chore(utils): start adding tests for changed utils
Browse files Browse the repository at this point in the history
  • Loading branch information
knagaitsev committed Jul 7, 2020
1 parent 76c5a03 commit ac17350
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/fixtures/webpack.public-path.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const path = require('path');

module.exports = {
mode: 'development',
context: path.resolve(__dirname),
entry: './simple.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, '../outputs/public-path'),
publicPath: '/public/path/',
},
infrastructureLogging: {
level: 'none'
},
stats: 'errors-warnings'
};
25 changes: 25 additions & 0 deletions test/helpers/listenAndCompile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default (app, compiler, done) => {
let complete = 0;
// wait until the app is listening and the done hook is called
const progress = () => {
complete += 1;
if (complete === 2) {
done();
}
};

const listen = app.listen((error) => {
if (error) {
// if there is an error, don't wait for the compilation to finish
return done(error);
}

return progress();
});

compiler.hooks.done.tap('wdm-test', () => {
return progress();
});

return listen;
};
8 changes: 8 additions & 0 deletions test/utils/getFilenameFromUrl.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// import getFilenameFromUrl from '../../src/utils/getFilenameFromUrl';

describe('getFilenameFromUrl', () => {
it('should work', () => {
// TODO
expect(true).toBeTruthy();
});
});
87 changes: 87 additions & 0 deletions test/utils/getPaths.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import path from 'path';

import express from 'express';

import middleware from '../../src';
import getPaths from '../../src/utils/getPaths';

import getCompiler from '../helpers/getCompiler';
import listenAndCompile from '../helpers/listenAndCompile';
import webpackSimpleConfig from '../fixtures/webpack.simple.config';
import webpackPublicPathConfig from '../fixtures/webpack.public-path.config';
import webpackMultiConfig from '../fixtures/webpack.array.config';

describe('getPaths', () => {
let instance;
let listen;
let app;
let compiler;

const configs = [
{
title: 'simple config',
config: webpackSimpleConfig,
expected: [
{
outputPath: path.resolve(__dirname, '../outputs/simple'),
publicPath: '',
},
],
},
{
title: 'publicPath config',
config: webpackPublicPathConfig,
expected: [
{
outputPath: path.resolve(__dirname, '../outputs/public-path'),
publicPath: '/public/path/',
},
],
},
{
title: 'multi config',
config: webpackMultiConfig,
expected: [
{
outputPath: path.resolve(__dirname, '../outputs/array/js1'),
publicPath: '/static-one/',
},
{
outputPath: path.resolve(__dirname, '../outputs/array/js2'),
publicPath: '/static-two/',
},
],
},
];

configs.forEach((config) => {
describe(config.title, () => {
beforeEach((done) => {
compiler = getCompiler(config.config);

instance = middleware(compiler);

app = express();
app.use(instance);

listen = listenAndCompile(app, compiler, done);
});

afterEach((done) => {
if (instance) {
instance.close();
}

if (listen) {
listen.close(done);
} else {
done();
}
});

it('should return correct paths', () => {
expect(getPaths(instance.context)).toEqual(config.expected);
});
});
});
});

0 comments on commit ac17350

Please sign in to comment.