-
Notifications
You must be signed in to change notification settings - Fork 5
/
watch.test.js
119 lines (106 loc) · 4.44 KB
/
watch.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const path = require('path');
const fs = require('fs');
const del = require('del');
const gulp = require('gulp');
const glob = require('glob');
const mkdirp = require('mkdirp');
const diff = require('lodash/difference')
const { slash } = require('../lib/utils');
const utils = require('./utils');
jest.mock('fancy-log');
const mpNpm = require('../index');
const unitFixtures = path.join(__dirname, 'fixtures/unit-test');
const unitExpected = path.join(__dirname, 'expected/unit-test');
const watchTemp = path.join(__dirname, 'temp/watch');
// config
jest.setTimeout(60000);
beforeEach(() => {
mkdirp.sync(watchTemp);
});
// tests
describe('watch 模式', () => {
test('修改 js 新增引入普通 npm 依赖', (done) => {
const target = 'import-normal-dep.js';
const expectedDir = 'import-normal-dep.js/';
const originContent = readUnitCaseFile(target);
const firstLine = originContent.split('\n')[0];
testWatch(target, expectedDir, firstLine, originContent, done);
});
test('修改 js 删除引入普通 npm 依赖', (done) => {
const target = 'import-normal-dep.js';
const expectedDir = 'import-normal-dep.js/';
const originContent = readUnitCaseFile(target);
const tempContent = `import mitt from 'mitt';\n` + originContent;
testWatch(target, expectedDir, tempContent, originContent, done);
});
test('修改 js 新增引入小程序专用 npm 组件', (done) => {
const target = 'import-special-dep.js';
const expectedDir = 'import-special-dep.js/';
const originContent = readUnitCaseFile(target);
const firstLine = originContent.split('\n')[0];
testWatch(target, expectedDir, firstLine, originContent, done);
});
});
// 测试文件监听
function testWatch(tempFile, expectedDir, oldContent, newContent, done) {
createTempFile(tempFile, oldContent);
const watcher = gulp.on('error', done)
.watch(tempFile, { cwd: watchTemp, delay: 500 }, (cb) => {
testTempCase(tempFile, expectedDir, (res) => {
watcher.close();
cb();
done(res);
});
});
updateTempFile(tempFile, newContent);
}
function readUnitCaseFile(originFile) {
const filePath = path.resolve(unitFixtures, originFile);
return fs.readFileSync(filePath, 'utf8');
}
function createTempFile(tempFile, content) {
const filePath = path.resolve(watchTemp, tempFile);
del.sync(filePath);
updateTempFile(filePath, content);
}
function updateTempFile(tempFile, content) {
const filePath = path.resolve(watchTemp, tempFile);
setTimeout(() => {
fs.writeFileSync(filePath, content);
}, 500);
}
// 执行并测试单元用例
function testTempCase(input, output, done, options = {}) {
const actualFiles = []; // 实际文件目录结构
const expectFiles = glob.sync(`${slash(output)}/**`, { cwd: unitExpected, base: unitExpected, absolute: true, nodir: true }); // 预期文件目录结构
gulp.src(slash(input), { cwd: watchTemp, base: watchTemp, nodir: true })
.pipe(mpNpm(options.mpNpmOptions))
.on('error', done)
.on('data', (file) => {
expect(file).not.toBeNil();
expect(file.path).not.toBeNil();
expect(file.base).not.toBeNil();
expect(file.contents).not.toBeNil();
// 找到预期文件
const relativePath = file.relative || path.relative(file.base, file.path);
const expectPath = path.resolve(path.join(unitExpected, output), relativePath);
expect(fs.existsSync(expectPath) ? expectPath : undefined)
.toBe(expectPath);
// 文件内容是否符合预期
const actualContent = utils.normaliseEOL(file.contents)
const expectContent = utils.normaliseEOL(fs.readFileSync(expectPath, 'utf8'));
if (actualContent.length > 5000 || expectContent.length > 5000) {
expect(actualContent.length).toBe(expectContent.length);
} else {
expect(actualContent).toBe(expectContent);
}
actualFiles.push(slash(expectPath));
})
.on('end', () => {
// 实际比预期多出的文件 (watch模式下不用对比)
//expect(diff(actualFiles, expectFiles)).toBeEmpty();
// 实际比预期缺少的文件
expect(diff(expectFiles, actualFiles)).toBeEmpty();
done();
});
}