/
loader.test.js
51 lines (40 loc) · 1.79 KB
/
loader.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
const { existsSync } = require('fs');
const { join, resolve } = require('path');
const rimraf = require('rimraf');
const stripAnsi = require('strip-ansi');
const { run, runPromptWithAnswers } = require('../utils/test-utils');
const firstPrompt = '? Loader name (my-loader)';
const ENTER = '\x0D';
const loaderName = 'test-loader';
const loaderPath = join(__dirname, loaderName);
describe('loader command', () => {
beforeAll(() => {
rimraf.sync(loaderPath);
});
it('Should ask the loader name when invoked', () => {
const { stdout, stderr } = run(__dirname, ['loader'], false);
expect(stdout).toBeTruthy();
expect(stderr).toBeFalsy();
expect(stripAnsi(stdout)).toContain(firstPrompt);
});
it('should scaffold loader template with a given name', async () => {
let { stdout } = await runPromptWithAnswers(__dirname, ['loader'], [`${loaderName}${ENTER}`]);
expect(stripAnsi(stdout)).toContain(firstPrompt);
// Skip test in case installation fails
if (!existsSync(resolve(loaderPath, './yarn.lock'))) {
return;
}
// check if the output directory exists with the appropriate loader name
expect(existsSync(join(__dirname, loaderName))).toBeTruthy();
// All test files are scaffolded
const files = ['package.json', 'examples', 'src', 'test', 'src/index.js', 'examples/simple/webpack.config.js'];
files.forEach((file) => {
expect(existsSync(join(__dirname, `${loaderName}/${file}`))).toBeTruthy();
});
//check if the the generated plugin works successfully
const path = resolve(__dirname, './test-loader/examples/simple/');
({ stdout } = run(path, [], false));
expect(stdout).toContain('test-loader');
});
});