Skip to content

Commit 5dc8b06

Browse files
wip(tests): unit tests enhancements
1 parent afea8bf commit 5dc8b06

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

tests/basic.test.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,54 @@
1-
const { defaultFiles, getPage } = require('./testsTools.js');
1+
const { defaultFiles, createPage } = require('./testsTools.js');
22

3-
test('text-only tamplate', async () => {
43

5-
const page = await getPage({
4+
test('text-only template', async () => {
5+
6+
const { page, output } = await createPage({
67
files: {
78
...defaultFiles,
89
'/component.vue': `
910
<template>
10-
Hello World ! <b id="done"/>
11+
Hello World !
1112
</template>
1213
`
1314
}
1415
});
1516

16-
await page.waitForSelector('#done');
1717
await expect(page.$eval('#app', el => el.textContent.trim())).resolves.toBe('Hello World !');
18+
1819
await page.close();
1920
});
2021

2122

2223
test('properly detect and reports errors in template', async () => {
2324

24-
const page = await getPage({
25+
const { page, output } = await createPage({
2526
files: {
2627
...defaultFiles,
2728
'/component.vue': `
2829
<template>
29-
Hello World ! {{ msg } <b id="done"/>
30+
Hello World ! {{ msg }
3031
</template>
3132
`
3233
}
3334
});
3435

35-
await page.waitForSelector('#done');
36-
await expect(page.console.some(e => e.type === 'error' && e.content[0] === 'SFC template')).toBe(true);
36+
//await page.waitForSelector('#done');
37+
await expect(output.some(e => e.type === 'error' && e.content[0] === 'SFC template')).toBe(true);
38+
//await new Promise(resolve => page.on('consoleValues', ({ type, args }) => type === 'error' && args[0] === 'SFC template' && resolve() ));
39+
3740
await page.close();
3841
});
3942

4043

4144
test('properly detect and reports errors in style', async () => {
4245

43-
const page = await getPage({
46+
const { page, output } = await createPage({
4447
files: {
4548
...defaultFiles,
4649
'/component.vue': `
4750
<template>
48-
Hello World ! <b id="done"/>
51+
Hello World !
4952
</template>
5053
<style>
5154
body
@@ -56,15 +59,15 @@ test('properly detect and reports errors in style', async () => {
5659
}
5760
});
5861

59-
await page.waitForSelector('#done');
60-
await expect(page.console.some(e => e.type === 'error' && e.content[0] === 'SFC style')).toBe(true);
62+
await expect(output.some(e => e.type === 'error' && e.content[0] === 'SFC style')).toBe(true);
63+
6164
await page.close();
6265
});
6366

6467

6568
test('properly detect and reports errors in script', async () => {
6669

67-
const page = await getPage({
70+
const { page, output } = await createPage({
6871
files: {
6972
...defaultFiles,
7073
'/component.vue': `
@@ -75,21 +78,21 @@ test('properly detect and reports errors in script', async () => {
7578
}
7679
});
7780

78-
await new Promise(resolve => setTimeout(resolve, 500));
79-
await expect(page.console.some(e => e.type === 'error' && e.content[0] === 'SFC script')).toBe(true);
81+
await expect(output.some(e => e.type === 'error' && e.content[0] === 'SFC script')).toBe(true);
82+
8083
await page.close();
8184
});
8285

8386

8487

8588
test('all blocks', async () => {
8689

87-
const page = await getPage({
90+
const { page, output } = await createPage({
8891
files: {
8992
...defaultFiles,
9093
'/component.vue': `
9194
<template>
92-
<b>Hello {{ msg }} !</b><b id="done"/>
95+
<b>Hello {{ msg }} !</b>
9396
</template>
9497
<style scoped>
9598
b { color: red; }
@@ -103,15 +106,15 @@ test('all blocks', async () => {
103106
}
104107
});
105108

106-
await new Promise(resolve => setTimeout(resolve, 500));
107-
await expect(!page.console.some(e => e.type === 'error')).toBe(true);
109+
await expect(!output.some(e => e.type === 'error')).toBe(true);
110+
108111
await page.close();
109112
});
110113

111114

112115
test('invalid require', async () => {
113116

114-
const page = await getPage({
117+
const { page, output } = await createPage({
115118
files: {
116119
...defaultFiles,
117120
'/component.vue': `
@@ -122,8 +125,8 @@ test('invalid require', async () => {
122125
}
123126
});
124127

125-
await new Promise(resolve => setTimeout(resolve, 1500));
126-
await expect(page.console.some(e => e.type === 'pageerror' && String(e.content).includes('HttpError') )).toBe(true);
128+
await expect(output.some(e => e.type === 'pageerror' && String(e.content).includes('HttpError') )).toBe(true);
129+
127130
await page.close();
128131
});
129132

tests/testsTools.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const mime = require('mime-types');
66

77
const local = new URL('http://local/');
88

9-
async function getPage({ files }) {
9+
async function createPage({ files }) {
1010

1111
async function getFile(url, encoding) {
1212

@@ -53,27 +53,32 @@ async function getPage({ files }) {
5353
}
5454
});
5555

56-
page.console = [];
56+
const output = [];
5757

58-
//page.on('console', msg => page.console.push({ type: msg.type(), content: msg.text() }) );
59-
page.on('console', async msg => page.console.push({ type: msg.type(), content: await Promise.all( msg.args().map(e => e.jsonValue()) ) }) );
60-
page.on('pageerror', error => page.console.push({ type: 'pageerror', content: error }) );
58+
page.on('console', async msg => output.push({ type: msg.type(), content: await Promise.all( msg.args().map(e => e.jsonValue()) ) }) );
59+
page.on('pageerror', error => output.push({ type: 'pageerror', content: error }) );
6160

6261
page.on('error', msg => console.log('ERROR', msg));
6362

6463
//page.done = new Promise(resolve => page.exposeFunction('_done', resolve));
6564

6665
await page.goto(new URL('/index.html', local));
6766

68-
return page;
67+
await new Promise(resolve => setTimeout(resolve, 250));
68+
69+
return { page, output };
6970
}
7071

7172
let browser;
7273

7374
beforeAll(async () => {
7475

76+
if ( browser )
77+
return browser;
78+
7579
browser = await puppeteer.launch({
76-
headless: !false,
80+
headless: true,
81+
pipe: true,
7782
args: [
7883
'--incognito',
7984
'--disable-gpu',
@@ -94,11 +99,12 @@ afterAll(async () => {
9499

95100
const defaultFiles = {
96101
'/vue3-sfc-loader.js': Fs.readFileSync(Path.join(__dirname, '../dist/vue3-sfc-loader.js'), { encoding: 'utf-8' }),
102+
'/vue': Fs.readFileSync(Path.join(__dirname, '../node_modules/vue/dist/vue.global.js'), { encoding: 'utf-8' }),
97103
'/index.html': `
98104
<!DOCTYPE html>
99105
<html><body>
100106
<div id="app"></div>
101-
<script src="https://unpkg.com/vue@next"></script>
107+
<script src="vue"></script>
102108
<script src="vue3-sfc-loader.js"></script>
103109
<script>
104110
@@ -114,9 +120,6 @@ const defaultFiles = {
114120
name: {
115121
value: this.constructor.name,
116122
},
117-
statusCode: {
118-
value: res.statusCode,
119-
},
120123
url: {
121124
value: url,
122125
},
@@ -165,5 +168,5 @@ const defaultFiles = {
165168

166169
module.exports = {
167170
defaultFiles,
168-
getPage,
171+
createPage,
169172
}

0 commit comments

Comments
 (0)