Skip to content

Commit e418c34

Browse files
author
Decebal Dobrica
authored
test: refactor tests with module-test-utils (#80)
1 parent d320fb0 commit e418c34

File tree

4 files changed

+414
-145
lines changed

4 files changed

+414
-145
lines changed

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@
3939
"@commitlint/cli": "latest",
4040
"@commitlint/config-conventional": "latest",
4141
"@nuxtjs/eslint-config": "latest",
42-
"codecov": "latest",
42+
"@nuxtjs/module-test-utils": "latest",
4343
"eslint": "latest",
44-
"get-port": "latest",
4544
"husky": "latest",
4645
"jest": "latest",
4746
"nuxt-edge": "latest",
48-
"request": "latest",
49-
"request-promise-native": "latest",
5047
"standard-version": "latest"
5148
}
5249
}

test/middleware.test.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,27 @@
1-
jest.setTimeout(60000)
21
jest.mock('async-cache', () => {
32
return jest.fn().mockImplementation(() => {
43
return { get: () => { throw new Error('Error on create feed') } }
54
})
65
})
76

8-
const { Nuxt, Builder } = require('nuxt-edge')
9-
const request = require('request-promise-native')
10-
const getPort = require('get-port')
7+
const { setup, loadConfig, get } = require('@nuxtjs/module-test-utils')
118
const logger = require('../lib/logger')
129
const { createFeed } = require('./feed-options')
1310

14-
const config = require('./fixture/nuxt.config')
15-
config.dev = false
16-
17-
let nuxt, port
18-
1911
logger.mockTypes(() => jest.fn())
2012

21-
const url = path => `http://localhost:${port}${path}`
22-
const get = path => request(url(path))
23-
2413
describe('middleware', () => {
14+
let nuxt
15+
2516
beforeAll(async () => {
26-
nuxt = new Nuxt({
27-
...config,
17+
({ nuxt } = await setup({
18+
...loadConfig(__dirname),
19+
dev: false,
2820
feed: [
2921
{ ...createFeed(), ...{ path: '/feed-error.xml' } }
3022
]
31-
})
32-
await nuxt.ready()
33-
await new Builder(nuxt).build()
34-
port = await getPort()
35-
await nuxt.listen(port)
36-
})
23+
}))
24+
}, 60000)
3725

3826
beforeEach(() => {
3927
logger.clear()

test/module.test.js

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,18 @@ jest.setTimeout(60000)
22

33
const { resolve, join } = require('path')
44
const { readFileSync } = require('fs')
5-
const { Nuxt, Builder, Generator } = require('nuxt-edge')
6-
const request = require('request-promise-native')
7-
const getPort = require('get-port')
5+
const { setup, generate, loadConfig, get } = require('@nuxtjs/module-test-utils')
86
const logger = require('../lib/logger')
97
const { createFeed, feedOptions } = require('./feed-options')
108

11-
const config = require('./fixture/nuxt.config')
9+
const config = loadConfig(__dirname)
1210
config.dev = false
1311

14-
let nuxt, port
15-
1612
logger.mockTypes(() => jest.fn())
1713

18-
const url = path => `http://localhost:${port}${path}`
19-
const get = path => request(url(path))
20-
21-
const setupNuxt = async (config) => {
22-
const nuxt = new Nuxt(config)
23-
await nuxt.ready()
24-
await new Builder(nuxt).build()
25-
port = await getPort()
26-
await nuxt.listen(port)
27-
28-
return nuxt
29-
}
30-
3114
describe('module', () => {
15+
let nuxt
16+
3217
beforeEach(() => {
3318
logger.clear()
3419
})
@@ -40,50 +25,51 @@ describe('module', () => {
4025
})
4126

4227
test('generate simple rss', async () => {
43-
nuxt = new Nuxt(config)
44-
await nuxt.ready()
45-
46-
const builder = new Builder(nuxt)
47-
const generator = new Generator(nuxt, builder)
48-
await generator.generate()
28+
({ nuxt } = await generate({
29+
...config,
30+
feed: [
31+
{ ...createFeed(), ...{ path: '/feed.xml' } },
32+
{ ...createFeed(), ...{ path: '/feed2.xml' } }
33+
]
34+
}))
4935

5036
const filePath = resolve(nuxt.options.rootDir, join(nuxt.options.generate.dir, 'feed.xml'))
5137
expect(readFileSync(filePath, { encoding: 'utf8' })).toMatchSnapshot()
5238
})
5339

5440
test('simple rss', async () => {
55-
nuxt = await setupNuxt(config)
41+
({ nuxt } = await setup(config))
5642

5743
const html = await get('/feed.xml')
5844
expect(html).toMatchSnapshot()
5945
})
6046

6147
test('simple atom', async () => {
62-
nuxt = await setupNuxt({
48+
({ nuxt } = await setup({
6349
...config,
6450
feed: [
6551
createFeed('atom1')
6652
]
67-
})
53+
}))
6854

6955
const html = await get('/feed.xml')
7056
expect(html).toMatchSnapshot()
7157
})
7258

7359
test('simple json', async () => {
74-
nuxt = await setupNuxt({
60+
({ nuxt } = await setup({
7561
...config,
7662
feed: [
7763
createFeed('json1')
7864
]
79-
})
65+
}))
8066

8167
const html = await get('/feed.xml')
8268
expect(html).toMatchSnapshot()
8369
})
8470

8571
test('non-latin rss', async () => {
86-
nuxt = await setupNuxt({
72+
({ nuxt } = await setup({
8773
...config,
8874
feed: [
8975
{
@@ -103,14 +89,14 @@ describe('module', () => {
10389
type: 'rss2'
10490
}
10591
]
106-
})
92+
}))
10793

10894
const html = await get('/feed.xml')
10995
expect(html).toMatchSnapshot()
11096
})
11197

11298
test('object rss', async () => {
113-
nuxt = await setupNuxt({
99+
({ nuxt } = await setup({
114100
...config,
115101
feed: {
116102
data: { title: 'Feed Title' },
@@ -120,14 +106,14 @@ describe('module', () => {
120106
},
121107
type: 'rss2'
122108
}
123-
})
109+
}))
124110

125111
const html = await get('/feed.xml')
126112
expect(html).toMatchSnapshot()
127113
})
128114

129115
test('factory rss', async () => {
130-
nuxt = await setupNuxt({
116+
({ nuxt } = await setup({
131117
...config,
132118
feed: {
133119
data: { title: 'Feed Title' },
@@ -140,30 +126,30 @@ describe('module', () => {
140126
type: 'rss2'
141127
})
142128
}
143-
})
129+
}))
144130

145131
const html = await get('/feed.xml')
146132
expect(html).toMatchSnapshot()
147133
})
148134

149135
test('function rss', async () => {
150-
nuxt = await setupNuxt({
136+
({ nuxt } = await setup({
151137
...config,
152138
feed: () => createFeed()
153-
})
139+
}))
154140

155141
const html = await get('/feed.xml')
156142
expect(html).toMatchSnapshot()
157143
})
158144

159145
test('multi rss', async () => {
160-
nuxt = await setupNuxt({
146+
({ nuxt } = await setup({
161147
...config,
162148
feed: [
163149
{ ...createFeed(), ...{ path: '/feed1.xml' } },
164150
{ ...createFeed(), ...{ path: '/feed2.xml' } }
165151
]
166-
})
152+
}))
167153

168154
const html1 = await get('/feed1.xml')
169155
expect(html1).toMatchSnapshot()
@@ -173,12 +159,12 @@ describe('module', () => {
173159
})
174160

175161
test('no type set', async () => {
176-
nuxt = await setupNuxt({
162+
({ nuxt } = await setup({
177163
...config,
178164
feed: [
179165
{}
180166
]
181-
})
167+
}))
182168

183169
const html = await get('/feed.xml')
184170
expect(html).toMatchSnapshot()
@@ -187,7 +173,7 @@ describe('module', () => {
187173
})
188174

189175
test('error on create feed', async () => {
190-
nuxt = await setupNuxt({
176+
({ nuxt } = await setup({
191177
...config,
192178
feed: [
193179
{
@@ -197,7 +183,7 @@ describe('module', () => {
197183
type: 'rss2'
198184
}
199185
]
200-
})
186+
}))
201187

202188
const html = await get('/feed.xml')
203189
expect(html).toMatchSnapshot()

0 commit comments

Comments
 (0)