Skip to content

Commit

Permalink
test: refactor tests with module-test-utils (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
Decebal Dobrica authored May 24, 2020
1 parent d320fb0 commit e418c34
Show file tree
Hide file tree
Showing 4 changed files with 414 additions and 145 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,11 @@
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
"@nuxtjs/eslint-config": "latest",
"codecov": "latest",
"@nuxtjs/module-test-utils": "latest",
"eslint": "latest",
"get-port": "latest",
"husky": "latest",
"jest": "latest",
"nuxt-edge": "latest",
"request": "latest",
"request-promise-native": "latest",
"standard-version": "latest"
}
}
28 changes: 8 additions & 20 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
jest.setTimeout(60000)
jest.mock('async-cache', () => {
return jest.fn().mockImplementation(() => {
return { get: () => { throw new Error('Error on create feed') } }
})
})

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

const config = require('./fixture/nuxt.config')
config.dev = false

let nuxt, port

logger.mockTypes(() => jest.fn())

const url = path => `http://localhost:${port}${path}`
const get = path => request(url(path))

describe('middleware', () => {
let nuxt

beforeAll(async () => {
nuxt = new Nuxt({
...config,
({ nuxt } = await setup({
...loadConfig(__dirname),
dev: false,
feed: [
{ ...createFeed(), ...{ path: '/feed-error.xml' } }
]
})
await nuxt.ready()
await new Builder(nuxt).build()
port = await getPort()
await nuxt.listen(port)
})
}))
}, 60000)

beforeEach(() => {
logger.clear()
Expand Down
74 changes: 30 additions & 44 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,18 @@ jest.setTimeout(60000)

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

const config = require('./fixture/nuxt.config')
const config = loadConfig(__dirname)
config.dev = false

let nuxt, port

logger.mockTypes(() => jest.fn())

const url = path => `http://localhost:${port}${path}`
const get = path => request(url(path))

const setupNuxt = async (config) => {
const nuxt = new Nuxt(config)
await nuxt.ready()
await new Builder(nuxt).build()
port = await getPort()
await nuxt.listen(port)

return nuxt
}

describe('module', () => {
let nuxt

beforeEach(() => {
logger.clear()
})
Expand All @@ -40,50 +25,51 @@ describe('module', () => {
})

test('generate simple rss', async () => {
nuxt = new Nuxt(config)
await nuxt.ready()

const builder = new Builder(nuxt)
const generator = new Generator(nuxt, builder)
await generator.generate()
({ nuxt } = await generate({
...config,
feed: [
{ ...createFeed(), ...{ path: '/feed.xml' } },
{ ...createFeed(), ...{ path: '/feed2.xml' } }
]
}))

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

test('simple rss', async () => {
nuxt = await setupNuxt(config)
({ nuxt } = await setup(config))

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

test('simple atom', async () => {
nuxt = await setupNuxt({
({ nuxt } = await setup({
...config,
feed: [
createFeed('atom1')
]
})
}))

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

test('simple json', async () => {
nuxt = await setupNuxt({
({ nuxt } = await setup({
...config,
feed: [
createFeed('json1')
]
})
}))

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

test('non-latin rss', async () => {
nuxt = await setupNuxt({
({ nuxt } = await setup({
...config,
feed: [
{
Expand All @@ -103,14 +89,14 @@ describe('module', () => {
type: 'rss2'
}
]
})
}))

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

test('object rss', async () => {
nuxt = await setupNuxt({
({ nuxt } = await setup({
...config,
feed: {
data: { title: 'Feed Title' },
Expand All @@ -120,14 +106,14 @@ describe('module', () => {
},
type: 'rss2'
}
})
}))

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

test('factory rss', async () => {
nuxt = await setupNuxt({
({ nuxt } = await setup({
...config,
feed: {
data: { title: 'Feed Title' },
Expand All @@ -140,30 +126,30 @@ describe('module', () => {
type: 'rss2'
})
}
})
}))

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

test('function rss', async () => {
nuxt = await setupNuxt({
({ nuxt } = await setup({
...config,
feed: () => createFeed()
})
}))

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

test('multi rss', async () => {
nuxt = await setupNuxt({
({ nuxt } = await setup({
...config,
feed: [
{ ...createFeed(), ...{ path: '/feed1.xml' } },
{ ...createFeed(), ...{ path: '/feed2.xml' } }
]
})
}))

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

test('no type set', async () => {
nuxt = await setupNuxt({
({ nuxt } = await setup({
...config,
feed: [
{}
]
})
}))

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

test('error on create feed', async () => {
nuxt = await setupNuxt({
({ nuxt } = await setup({
...config,
feed: [
{
Expand All @@ -197,7 +183,7 @@ describe('module', () => {
type: 'rss2'
}
]
})
}))

const html = await get('/feed.xml')
expect(html).toMatchSnapshot()
Expand Down
Loading

0 comments on commit e418c34

Please # to comment.