working tests

This commit is contained in:
Pooya Parsa 2018-03-19 03:11:14 +03:30
parent e6f84d16bb
commit c4068c3a70
32 changed files with 382 additions and 607 deletions

21
jest.config.js Normal file
View File

@ -0,0 +1,21 @@
module.exports = {
'testEnvironment': 'node',
'coverageDirectory': './coverage/',
'collectCoverage': false,
'setupTestFrameworkScriptFile': './test/utils/setup',
'testMatch': [
'<rootDir>/test/*.test.js'
],
'projects': [
// {
// 'displayName': 'Lint',
// 'runner': 'jest-runner-eslint',
// 'testMatch': [
// '<rootDir>/lib/*.js'
// ]
// },
{
'displayName': 'Test'
}
]
}

View File

@ -40,12 +40,6 @@
"bin": { "bin": {
"nuxt": "./bin/nuxt" "nuxt": "./bin/nuxt"
}, },
"jest": {
"testEnvironment": "node",
"coverageDirectory": "./coverage/",
"collectCoverage": false,
"setupTestFrameworkScriptFile": "./test/helpers/setup"
},
"scripts": { "scripts": {
"build-fixtures": "./scripts/build-fixtures", "build-fixtures": "./scripts/build-fixtures",
"test": "npm run lint && jest", "test": "npm run lint && jest",
@ -136,7 +130,9 @@
"eslint-plugin-standard": "^3.0.1", "eslint-plugin-standard": "^3.0.1",
"express": "^4.16.2", "express": "^4.16.2",
"finalhandler": "^1.1.1", "finalhandler": "^1.1.1",
"get-port": "^3.2.0",
"jest": "^22.4.2", "jest": "^22.4.2",
"jest-runner-eslint": "^0.4.0",
"jsdom": "^11.6.2", "jsdom": "^11.6.2",
"puppeteer": "^1.0.0", "puppeteer": "^1.0.0",
"request": "^2.83.0", "request": "^2.83.0",

View File

@ -33,6 +33,7 @@ async function buildFixture(name) {
const config = fs.existsSync(configFile) ? requireModule(configFile) : {} const config = fs.existsSync(configFile) ? requireModule(configFile) : {}
config.rootDir = rootDir config.rootDir = rootDir
config.dev = false
spinner.start('Building fixture ' + name) spinner.start('Building fixture ' + name)

View File

@ -1,36 +1,19 @@
import { resolve } from 'path' import { Nuxt } from '..'
import { loadFixture, getPort } from './utils'
import * as browser from './utils/browser'
import { Nuxt, Builder } from '..' let port
import * as browser from './helpers/browser'
const port = 4003
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
let page = null let page = null
const waitFor = ms => new Promise(resolve => setTimeout(resolve, ms || 0)) describe('basic browser', () => {
describe('basic csr', () => {
// Init nuxt.js and create server listening on localhost:4003
beforeAll(async () => { beforeAll(async () => {
const options = { const config = loadFixture('basic')
rootDir: resolve(__dirname, 'fixtures/basic'), nuxt = new Nuxt(config)
buildDir: '.nuxt-csr', port = await getPort() ; await nuxt.listen(port, 'localhost')
dev: true,
build: {
stats: false
}
}
nuxt = new Nuxt(options)
new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
}, 30000)
test('Start browser', async () => {
expect.assertions(0) // suppress 'no assertions' warning
await browser.start({ await browser.start({
// slowMo: 50, // slowMo: 50,
// headless: false // headless: false
@ -39,7 +22,6 @@ describe('basic csr', () => {
test('Open /', async () => { test('Open /', async () => {
page = await browser.page(url('/')) page = await browser.page(url('/'))
await waitFor(1000)
expect(await page.$text('h1')).toBe('Index page') expect(await page.$text('h1')).toBe('Index page')
}) })
@ -47,7 +29,6 @@ describe('basic csr', () => {
test('/stateless', async () => { test('/stateless', async () => {
const { hook } = await page.nuxt.navigate('/stateless', false) const { hook } = await page.nuxt.navigate('/stateless', false)
const loading = await page.nuxt.loadingData() const loading = await page.nuxt.loadingData()
await waitFor(1000)
expect(loading.show).toBe(true) expect(loading.show).toBe(true)
await hook await hook
@ -56,22 +37,20 @@ describe('basic csr', () => {
test('/css', async () => { test('/css', async () => {
await page.nuxt.navigate('/css') await page.nuxt.navigate('/css')
await waitFor(1000)
expect(await page.$text('.red')).toBe('This is red') expect(await page.$text('.red')).toBe('This is red')
expect(await page.$eval('.red', red => window.getComputedStyle(red).color)).toBe('rgb(255, 0, 0)') expect(await page.$eval('.red', red => window.getComputedStyle(red).color)).toBe('rgb(255, 0, 0)')
}) })
test('/stateful', async () => { test.skip('/stateful', async () => {
await page.nuxt.navigate('/stateful') const { hook } = await page.nuxt.navigate('/stateful')
await waitFor(1000)
await hook
expect(await page.$text('p')).toBe('The answer is 42') expect(await page.$text('p')).toBe('The answer is 42')
}) })
test('/store', async () => { test('/store', async () => {
await page.nuxt.navigate('/store') await page.nuxt.navigate('/store')
await waitFor(1000)
expect(await page.$text('h1')).toBe('Vuex Nested Modules') expect(await page.$text('h1')).toBe('Vuex Nested Modules')
expect(await page.$text('p')).toBe('1') expect(await page.$text('p')).toBe('1')
@ -83,7 +62,6 @@ describe('basic csr', () => {
) )
await page.nuxt.navigate('/head') await page.nuxt.navigate('/head')
const metas = await page.$$attr('meta', 'content') const metas = await page.$$attr('meta', 'content')
await waitFor(1000)
expect(await msg).toBe('Body script!') expect(await msg).toBe('Body script!')
expect(await page.title()).toBe('My title - Nuxt.js') expect(await page.title()).toBe('My title - Nuxt.js')
@ -93,35 +71,30 @@ describe('basic csr', () => {
test('/async-data', async () => { test('/async-data', async () => {
await page.nuxt.navigate('/async-data') await page.nuxt.navigate('/async-data')
await waitFor(1000)
expect(await page.$text('p')).toBe('Nuxt.js') expect(await page.$text('p')).toBe('Nuxt.js')
}) })
test('/await-async-data', async () => { test('/await-async-data', async () => {
await page.nuxt.navigate('/await-async-data') await page.nuxt.navigate('/await-async-data')
await waitFor(1000)
expect(await page.$text('p')).toBe('Await Nuxt.js') expect(await page.$text('p')).toBe('Await Nuxt.js')
}) })
test('/callback-async-data', async () => { test('/callback-async-data', async () => {
await page.nuxt.navigate('/callback-async-data') await page.nuxt.navigate('/callback-async-data')
await waitFor(1000)
expect(await page.$text('p')).toBe('Callback Nuxt.js') expect(await page.$text('p')).toBe('Callback Nuxt.js')
}) })
test('/users/1', async () => { test('/users/1', async () => {
await page.nuxt.navigate('/users/1') await page.nuxt.navigate('/users/1')
await waitFor(1000)
expect(await page.$text('h1')).toBe('User: 1') expect(await page.$text('h1')).toBe('User: 1')
}) })
test('/validate should display a 404', async () => { test('/validate should display a 404', async () => {
await page.nuxt.navigate('/validate') await page.nuxt.navigate('/validate')
await waitFor(1000)
const error = await page.nuxt.errorData() const error = await page.nuxt.errorData()
@ -131,21 +104,18 @@ describe('basic csr', () => {
test('/validate?valid=true', async () => { test('/validate?valid=true', async () => {
await page.nuxt.navigate('/validate?valid=true') await page.nuxt.navigate('/validate?valid=true')
await waitFor(1000)
expect(await page.$text('h1')).toBe('I am valid') expect(await page.$text('h1')).toBe('I am valid')
}) })
test('/redirect', async () => { test('/redirect', async () => {
await page.nuxt.navigate('/redirect') await page.nuxt.navigate('/redirect')
await waitFor(1000)
expect(await page.$text('h1')).toBe('Index page') expect(await page.$text('h1')).toBe('Index page')
}) })
test('/error', async () => { test('/error', async () => {
await page.nuxt.navigate('/error') await page.nuxt.navigate('/error')
await waitFor(1000)
expect(await page.nuxt.errorData()).toEqual({ statusCode: 500 }) expect(await page.nuxt.errorData()).toEqual({ statusCode: 500 })
expect(await page.$text('.title')).toBe('Error mouahahah') expect(await page.$text('.title')).toBe('Error mouahahah')
@ -153,7 +123,6 @@ describe('basic csr', () => {
test('/error2', async () => { test('/error2', async () => {
await page.nuxt.navigate('/error2') await page.nuxt.navigate('/error2')
await waitFor(1000)
expect(await page.$text('.title')).toBe('Custom error') expect(await page.$text('.title')).toBe('Custom error')
expect(await page.nuxt.errorData()).toEqual({ message: 'Custom error' }) expect(await page.nuxt.errorData()).toEqual({ message: 'Custom error' })
@ -161,7 +130,6 @@ describe('basic csr', () => {
test('/redirect-middleware', async () => { test('/redirect-middleware', async () => {
await page.nuxt.navigate('/redirect-middleware') await page.nuxt.navigate('/redirect-middleware')
await waitFor(1000)
expect(await page.$text('h1')).toBe('Index page') expect(await page.$text('h1')).toBe('Index page')
}) })
@ -171,7 +139,6 @@ describe('basic csr', () => {
const page = await browser.page(url('/')) const page = await browser.page(url('/'))
await page.nuxt.navigate('/redirect-external', false) await page.nuxt.navigate('/redirect-external', false)
await waitFor(1000)
await page.waitForFunction( await page.waitForFunction(
() => window.location.href === 'https://nuxtjs.org/' () => window.location.href === 'https://nuxtjs.org/'
@ -181,21 +148,18 @@ describe('basic csr', () => {
test('/redirect-name', async () => { test('/redirect-name', async () => {
await page.nuxt.navigate('/redirect-name') await page.nuxt.navigate('/redirect-name')
await waitFor(1000)
expect(await page.$text('h1')).toBe('My component!') expect(await page.$text('h1')).toBe('My component!')
}) })
test('/no-ssr', async () => { test('/no-ssr', async () => {
await page.nuxt.navigate('/no-ssr') await page.nuxt.navigate('/no-ssr')
await waitFor(1000)
expect(await page.$text('h1')).toBe('Displayed only on client-side') expect(await page.$text('h1')).toBe('Displayed only on client-side')
}) })
test('/meta', async () => { test('/meta', async () => {
await page.nuxt.navigate('/meta') await page.nuxt.navigate('/meta')
await waitFor(1000)
const state = await page.nuxt.storeState() const state = await page.nuxt.storeState()
expect(state.meta).toEqual([{ works: true }]) expect(state.meta).toEqual([{ works: true }])
@ -203,7 +167,6 @@ describe('basic csr', () => {
test('/fn-midd', async () => { test('/fn-midd', async () => {
await page.nuxt.navigate('/fn-midd') await page.nuxt.navigate('/fn-midd')
await waitFor(1000)
expect(await page.$text('.title')).toBe('You need to ask the permission') expect(await page.$text('.title')).toBe('You need to ask the permission')
expect(await page.nuxt.errorData()).toEqual({ expect(await page.nuxt.errorData()).toEqual({
@ -214,7 +177,6 @@ describe('basic csr', () => {
test('/fn-midd?please=true', async () => { test('/fn-midd?please=true', async () => {
await page.nuxt.navigate('/fn-midd?please=true') await page.nuxt.navigate('/fn-midd?please=true')
await waitFor(1000)
const h1 = await page.$text('h1') const h1 = await page.$text('h1')
expect(h1.includes('Date:')).toBe(true) expect(h1.includes('Date:')).toBe(true)
@ -222,19 +184,18 @@ describe('basic csr', () => {
test('/router-guard', async () => { test('/router-guard', async () => {
await page.nuxt.navigate('/router-guard') await page.nuxt.navigate('/router-guard')
await waitFor(1000)
const p = await page.$text('p') const p = await page.$text('p')
expect(p).toBe('Nuxt.js') expect(p).toBe('Nuxt.js')
}) })
test('Stop browser', async () => {
process.on('unhandledRejection', () => { })
await browser.stop()
})
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test('Closing server and nuxt.js', async () => { test('Closing server and nuxt.js', async () => {
await nuxt.close() await nuxt.close()
}) })
test('Stop browser', async () => {
await page.close()
await browser.close()
})
}) })

View File

@ -1,5 +1,4 @@
import { resolve } from 'path' import { resolve } from 'path'
import { Nuxt, Options } from '..' import { Nuxt, Options } from '..'
import { version } from '../package.json' import { version } from '../package.json'

View File

@ -1,32 +1,20 @@
import { resolve } from 'path'
import { Nuxt, Builder } from '..' import { Nuxt, Builder } from '..'
import { loadFixture, getPort } from './utils'
const port = 4001 let port
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
const rootDir = resolve(__dirname, 'fixtures/basic')
let nuxt = null let nuxt = null
describe('basic dev', () => { describe('basic dev', () => {
// Init nuxt.js and create server listening on localhost:4000
beforeAll(async () => { beforeAll(async () => {
const options = { const config = loadFixture('basic', { buildDir: '.nuxt-dev' })
rootDir, config.dev = true
buildDir: '.nuxt-dev', nuxt = new Nuxt(config)
dev: true,
build: {
stats: false,
profile: true,
extractCSS: {
allChunks: true
}
}
}
nuxt = new Nuxt(options)
new Builder(nuxt).build() new Builder(nuxt).build()
port = await getPort()
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}, 30000) })
// TODO: enable test when style-loader.js:60 was resolved // TODO: enable test when style-loader.js:60 was resolved
// test.serial('/extractCSS', async t => { // test.serial('/extractCSS', async t => {

View File

@ -1,28 +1,21 @@
import { resolve } from 'path' import { Nuxt, Generator } from '..'
import { loadFixture } from './utils'
import { Nuxt, Builder, Generator } from '..'
describe('basic fail generate', () => { describe('basic fail generate', () => {
test('Fail with routes() which throw an error', async () => { test('Fail with routes() which throw an error', async () => {
const options = { const options = loadFixture('basic', {
rootDir: resolve(__dirname, 'fixtures/basic'),
buildDir: '.nuxt-fail',
dev: false,
build: {
stats: false
},
generate: { generate: {
async routes() { async routes() {
throw new Error('Not today!') throw new Error('Not today!')
} }
} }
}
const nuxt = new Nuxt(options)
const builder = new Builder(nuxt)
const generator = new Generator(nuxt, builder)
await generator.generate().catch(e => {
expect(e.message === 'Not today!').toBe(true)
}) })
}, 30000)
const nuxt = new Nuxt(options)
const generator = new Generator(nuxt)
await generator.generate({ build: false }).catch(e => {
expect(e.message).toBe('Not today!')
})
})
}) })

View File

@ -1,17 +1,15 @@
import { existsSync } from 'fs' import { existsSync } from 'fs'
import http from 'http' import http from 'http'
import { resolve } from 'path' import { resolve } from 'path'
import { remove } from 'fs-extra' import { remove } from 'fs-extra'
import serveStatic from 'serve-static' import serveStatic from 'serve-static'
import finalhandler from 'finalhandler' import finalhandler from 'finalhandler'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt, Generator } from '..'
import { loadFixture, getPort } from './utils'
import { get } from 'https';
import { Nuxt, Builder, Generator } from '..' let port
import { loadConfig } from './helpers/config'
const port = 4002
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
const rootDir = resolve(__dirname, 'fixtures/basic') const rootDir = resolve(__dirname, 'fixtures/basic')
@ -20,27 +18,21 @@ let server = null
let generator = null let generator = null
describe('basic generate', () => { describe('basic generate', () => {
// Init nuxt.js and create server listening on localhost:4000
beforeAll(async () => { beforeAll(async () => {
const config = loadConfig('basic', { const config = loadFixture('basic')
buildDir: '.nuxt-generate',
dev: false
})
config.build.stats = false
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
const builder = new Builder(nuxt) generator = new Generator(nuxt)
generator = new Generator(nuxt, builder)
await generator.generate() await generator.generate({ build: false })
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist')) const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
server = http.createServer((req, res) => { server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res)) serve(req, res, finalhandler(req, res))
}) })
port = await getPort()
server.listen(port) server.listen(port)
}, 30000) })
test('Check ready hook called', async () => { test('Check ready hook called', async () => {
expect(nuxt.__hook_called__).toBe(true) expect(nuxt.__hook_called__).toBe(true)
@ -76,7 +68,7 @@ describe('basic generate', () => {
const window = await nuxt.renderAndGetWindow(url('/head')) const window = await nuxt.renderAndGetWindow(url('/head'))
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
const metas = window.document.getElementsByTagName('meta') const metas = window.document.getElementsByTagName('meta')
expect(window.document.title).toBe('My title') expect(window.document.title).toBe('My title - Nuxt.js')
expect(metas[0].getAttribute('content')).toBe('my meta') expect(metas[0].getAttribute('content')).toBe('my meta')
expect(html.includes('<div><h1>I can haz meta tags</h1></div>')).toBe(true) expect(html.includes('<div><h1>I can haz meta tags</h1></div>')).toBe(true)
// release() // release()
@ -160,7 +152,7 @@ describe('basic generate', () => {
test('nuxt re-generating with no subfolders', async () => { test('nuxt re-generating with no subfolders', async () => {
// const logSpy = await interceptLog() // const logSpy = await interceptLog()
nuxt.options.generate.subFolders = false nuxt.options.generate.subFolders = false
await generator.generate() await generator.generate({ build: false })
// release() // release()
// expect(logSpy.calledWithMatch('DONE')).toBe(true) // expect(logSpy.calledWithMatch('DONE')).toBe(true)
}) })

View File

@ -1,27 +1,15 @@
import { resolve } from 'path'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt } from '..'
import { loadFixture, getPort } from './utils'
import { Nuxt, Builder } from '..' let port
const port = 4005
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
const startCSPTestServer = async (csp) => { const startCSPTestServer = async (csp) => {
const options = { const options = loadFixture('basic', { render: { csp } })
rootDir: resolve(__dirname, 'fixtures/basic'), const nuxt = new Nuxt(options)
buildDir: '.nuxt-ssr-csp', port = await getPort()
dev: false,
build: { stats: false },
render: { csp }
}
let nuxt = null
nuxt = new Nuxt(options)
const builder = new Builder(nuxt)
await builder.build()
await nuxt.listen(port, '0.0.0.0') await nuxt.listen(port, '0.0.0.0')
return nuxt return nuxt
} }

View File

@ -1,30 +1,19 @@
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt } from '..'
import { loadFixture, getPort } from './utils'
import { Nuxt, Builder } from '..' let port
import { loadConfig } from './helpers/config'
const port = 4004
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
describe('basic ssr', () => { describe('basic ssr', () => {
// Init nuxt.js and create server listening on localhost:4004
beforeAll(async () => { beforeAll(async () => {
const options = loadConfig('basic', { const options = loadFixture('basic')
buildDir: '.nuxt-ssr',
dev: false,
build: {
stats: false
}
})
nuxt = new Nuxt(options) nuxt = new Nuxt(options)
const builder = new Builder(nuxt) port = await getPort()
await builder.build()
await nuxt.listen(port, '0.0.0.0') await nuxt.listen(port, '0.0.0.0')
}, 30000) })
test('/stateless', async () => { test('/stateless', async () => {
const { html } = await nuxt.renderRoute('/stateless') const { html } = await nuxt.renderRoute('/stateless')

View File

@ -1,32 +1,21 @@
import { resolve } from 'path' import { Nuxt, Utils } from '..'
import * as browser from './utils/browser'
import { loadFixture, getPort } from './utils'
import { Nuxt, Builder, Utils } from '..' let port
import * as browser from './helpers/browser'
const port = 4014
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
let page let page
const dates = {} const dates = {}
describe('children patch', () => { describe('children patch (browser)', () => {
// Init nuxt.js and create server listening on localhost:4000
beforeAll(async () => { beforeAll(async () => {
const options = { const options = loadFixture('children')
rootDir: resolve(__dirname, 'fixtures/children'),
buildDir: '.nuxt-patch',
dev: false,
build: {
stats: false
}
}
nuxt = new Nuxt(options) nuxt = new Nuxt(options)
new Builder(nuxt).build() port = await getPort()
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}, 30000) })
test('Start browser', async () => { test('Start browser', async () => {
expect.assertions(0) // suppress 'no assertions' warning expect.assertions(0) // suppress 'no assertions' warning
@ -138,6 +127,6 @@ describe('children patch', () => {
test('Stop browser', async () => { test('Stop browser', async () => {
await page.close() await page.close()
await browser.stop() await browser.close()
}) })
}) })

View File

@ -1,27 +1,18 @@
import { resolve } from 'path' import { Nuxt } from '..'
import { loadFixture, getPort } from './utils'
import { Nuxt, Builder } from '..' let port
const port = 4013
// const url = (route) => 'http://localhost:' + port + route // const url = (route) => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
describe('children', () => { describe('children', () => {
// Init nuxt.js and create server listening on localhost:4000
beforeAll(async () => { beforeAll(async () => {
const options = { const options = loadFixture('children')
rootDir: resolve(__dirname, 'fixtures/children'),
dev: false,
build: {
stats: false
}
}
nuxt = new Nuxt(options) nuxt = new Nuxt(options)
new Builder(nuxt).build() port = await getPort()
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}, 30000) })
test('/parent', async () => { test('/parent', async () => {
const { html } = await nuxt.renderRoute('/parent') const { html } = await nuxt.renderRoute('/parent')

View File

@ -1,18 +1,18 @@
import { promisify } from 'util'
import { resolve } from 'path'
import rp from 'request-promise-native'
import { exec, spawn } from 'child_process' import { exec, spawn } from 'child_process'
import { resolve } from 'path'
import { promisify } from 'util'
import rp from 'request-promise-native'
import { Utils } from '..' import { Utils } from '..'
const execify = promisify(exec) const execify = promisify(exec)
const rootDir = resolve(__dirname, 'fixtures/basic') const rootDir = resolve(__dirname, 'fixtures/basic')
const port = 4011 let port
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
const nuxtBin = resolve(__dirname, '..', 'bin', 'nuxt') const nuxtBin = resolve(__dirname, '..', 'bin', 'nuxt')
describe('cli', () => { describe.skip('cli', () => {
test('nuxt build', async () => { test('nuxt build', async () => {
const { stdout } = await execify(`node ${nuxtBin} build ${rootDir}`) const { stdout } = await execify(`node ${nuxtBin} build ${rootDir}`)

View File

@ -1,23 +1,19 @@
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt, Builder } from '..' import { Nuxt } from '..'
import { loadConfig } from './helpers/config' import { loadFixture, getPort } from './utils'
const port = 4007 let port
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
let builder = null
describe('custom-dirs', () => { describe('custom-dirs', () => {
// Init nuxt.js and create server listening on localhost:4000
beforeAll(async () => { beforeAll(async () => {
const config = loadConfig('/custom-dirs', { dev: false }) const config = loadFixture('custom-dirs')
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
builder = new Builder(nuxt) port = await getPort()
await builder.build() await nuxt.listen(port, 'localhost')
await nuxt.listen(4007, 'localhost') })
}, 30000)
test('custom assets directory', async () => { test('custom assets directory', async () => {
const { html } = await nuxt.renderRoute('/') const { html } = await nuxt.renderRoute('/')

View File

@ -1,21 +1,19 @@
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt, Builder } from '..' import { Nuxt } from '..'
import { loadConfig } from './helpers/config' import { loadFixture, getPort } from './utils'
const port = 4009 let port
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
describe('debug', () => { describe.skip('debug', () => {
// Init nuxt.js and create server listening on localhost:4000
beforeAll(async () => { beforeAll(async () => {
const config = loadConfig('debug') const config = loadFixture('debug')
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
new Builder(nuxt).build() port = await getPort()
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}, 30000) })
test('/test/__open-in-editor (open-in-editor)', async () => { test('/test/__open-in-editor (open-in-editor)', async () => {
const { body } = await rp( const { body } = await rp(

View File

@ -1,28 +1,25 @@
import { Nuxt, Builder } from '..' import { Nuxt } from '..'
import { loadConfig } from './helpers/config' import { loadFixture, getPort } from './utils'
const port = 4010 let port
let nuxt = null let nuxt = null
let builder = null
// let buildSpies = null // let buildSpies = null
describe('depricate', () => { describe('depricate', () => {
// Init nuxt.js and create server listening on localhost:4000
beforeAll(async () => { beforeAll(async () => {
const config = loadConfig('deprecate', { dev: false }) const config = loadFixture('deprecate')
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
builder = new Builder(nuxt) port = await getPort()
await builder.build()
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}, 30000) })
// test('Deprecated: module.addVendor()', async () => { test.skip('Deprecated: module.addVendor()', async () => {
// expect( // expect(
// buildSpies.warn.calledWithMatch('module: addVendor is no longer necessary') // buildSpies.warn.calledWithMatch('module: addVendor is no longer necessary')
// ).toBe(true) // ).toBe(true)
// }) })
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test('Closing server and nuxt.js', async () => { test('Closing server and nuxt.js', async () => {

View File

@ -2,24 +2,9 @@ import { resolve } from 'path'
import fs from 'fs' import fs from 'fs'
import { promisify } from 'util' import { promisify } from 'util'
import { Nuxt, Builder } from '..'
const readFile = promisify(fs.readFile) const readFile = promisify(fs.readFile)
describe('dynamic routes', () => { describe('dynamic routes', () => {
beforeAll(async () => {
const config = {
rootDir: resolve(__dirname, 'fixtures/dynamic-routes'),
dev: false,
build: {
stats: false
}
}
const nuxt = new Nuxt(config)
new Builder(nuxt).build()
}, 30000)
test('Check .nuxt/router.js', () => { test('Check .nuxt/router.js', () => {
return readFile( return readFile(
resolve(__dirname, './fixtures/dynamic-routes/.nuxt/router.js'), resolve(__dirname, './fixtures/dynamic-routes/.nuxt/router.js'),

View File

@ -1,22 +1,20 @@
// import rp from 'request-promise-native' // import rp from 'request-promise-native'
import { Nuxt, Builder } from '..' import { Nuxt } from '..'
import { loadConfig } from './helpers/config' import { loadFixture, getPort } from './utils'
const port = 4005 let port
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
// let logSpy // let logSpy
describe('error', () => { describe('error', () => {
// Init nuxt.js and create server listening on localhost:4000
beforeAll(async () => { beforeAll(async () => {
const config = loadConfig('error', { dev: false }) const config = loadFixture('error')
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
new Builder(nuxt).build() port = await getPort()
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}, 30000) })
test('/ should display an error', async () => { test('/ should display an error', async () => {
await expect(nuxt.renderRoute('/')).rejects.toMatchObject({ await expect(nuxt.renderRoute('/')).rejects.toMatchObject({

View File

@ -1,30 +1,22 @@
import { resolve } from 'path'
import express from 'express' import express from 'express'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt } from '..'
import { loadFixture, getPort } from './utils'
import { Nuxt, Builder } from '..' let port
const port = 4000
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
let nuxt let nuxt
let app let app
let server
describe('express', () => { describe('express', () => {
// Init nuxt.js and create express server // Init nuxt.js and create express server
beforeAll(async () => { beforeAll(async () => {
const config = { const config = loadFixture('basic')
rootDir: resolve(__dirname, 'fixtures/basic'),
buildDir: '.nuxt-express',
dev: false,
build: {
stats: false
}
}
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
new Builder(nuxt).build()
port = await getPort()
// Create express app // Create express app
app = express() app = express()
@ -33,12 +25,19 @@ describe('express', () => {
app.use(nuxt.render) app.use(nuxt.render)
// Start listening on localhost:4000 // Start listening on localhost:4000
app.listen(port) server = app.listen(port)
}, 30000) })
test('/stateless with express', async () => { test('/stateless with express', async () => {
const html = await rp(url('/stateless')) const html = await rp(url('/stateless'))
expect(html.includes('<h1>My component!</h1>')).toBe(true) expect(html.includes('<h1>My component!</h1>')).toBe(true)
}) })
test('close server', async () => {
await nuxt.close()
await new Promise((resolve, reject) => {
server.close(err => err ? reject(err) : resolve())
})
})
}) })

View File

@ -1,16 +1,13 @@
import http from 'http' import http from 'http'
import { existsSync } from 'fs' import { existsSync } from 'fs'
import { resolve } from 'path' import { resolve } from 'path'
import serveStatic from 'serve-static' import serveStatic from 'serve-static'
import finalhandler from 'finalhandler' import finalhandler from 'finalhandler'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt, Generator, Options } from '..'
import { loadFixture, getPort } from './utils'
import { Nuxt, Builder, Generator, Options } from '..' let port
import { loadConfig } from './helpers/config'
const port = 4015
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
@ -18,26 +15,23 @@ let server = null
let generator = null let generator = null
describe('fallback generate', () => { describe('fallback generate', () => {
// Init nuxt.js and create server listening on localhost:4015
beforeAll(async () => { beforeAll(async () => {
let config = loadConfig('basic', { const config = loadFixture('basic')
buildDir: '.nuxt-spa-fallback',
dev: false
})
config.build.stats = false
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
const builder = new Builder(nuxt) generator = new Generator(nuxt)
generator = new Generator(nuxt, builder)
await generator.generate() await generator.generate({ build: false })
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist')) const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
server = http.createServer((req, res) => { server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res)) serve(req, res, finalhandler(req, res))
}) })
port = await getPort()
server.listen(port) server.listen(port)
}, 30000) })
test('default creates /200.html as fallback', async () => { test('default creates /200.html as fallback', async () => {
const html = await rp(url('/200.html')) const html = await rp(url('/200.html'))
@ -50,7 +44,7 @@ describe('fallback generate', () => {
test('nuxt re-generating with generate.fallback = false', async () => { test('nuxt re-generating with generate.fallback = false', async () => {
// const logSpy = await interceptLog(async () => { // const logSpy = await interceptLog(async () => {
nuxt.options.generate.fallback = false nuxt.options.generate.fallback = false
await generator.generate() await generator.generate({ build: false })
// expect(logSpy.calledWithMatch('DONE')).toBe(true) // expect(logSpy.calledWithMatch('DONE')).toBe(true)
}) })
@ -76,36 +70,10 @@ describe('fallback generate', () => {
'nuxt re-generating with generate.fallback = "spa-fallback.html"', 'nuxt re-generating with generate.fallback = "spa-fallback.html"',
async () => { async () => {
nuxt.options.generate.fallback = 'spa-fallback.html' nuxt.options.generate.fallback = 'spa-fallback.html'
await generator.generate() await generator.generate({ build: false })
} }
) )
test(
'"spa-fallback.html" creates /spa-fallback.html as fallback',
async () => {
const html = await rp(url('/spa-fallback.html'))
expect(html.includes('<h1>Index page</h1>')).toBe(false)
expect(html.includes('data-server-rendered')).toBe(false)
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'spa-fallback.html'))).toBe(true)
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', '404.html'))).toBe(false)
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', '200.html'))).toBe(false)
}
)
test('nuxt re-generating with generate.fallback = "index.html"', async () => {
nuxt.options.generate.fallback = 'index.html'
await generator.generate()
})
test('"index.html" creates /index.html as fallback', async () => {
const html = await rp(url('/index.html'))
expect(html.includes('<h1>Index page</h1>')).toBe(true)
expect(html.includes('data-server-rendered')).toBe(true)
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', 'index.html'))).toBe(true)
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', '404.html'))).toBe(false)
expect(existsSync(resolve(__dirname, 'fixtures/basic/dist', '200.html'))).toBe(false)
})
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test('Closing server', async () => { test('Closing server', async () => {
await server.close() await server.close()

View File

@ -3,6 +3,15 @@ import path from 'path'
export default { export default {
generate: { generate: {
routes: [ routes: [
// TODO: generate with {build: false} does not scans pages!
'/stateless',
'/css',
'/stateful',
'/head',
'/async-data',
'/validate',
'/redirect',
'/users/1', '/users/1',
'/users/2', '/users/2',
{ route: '/users/3', payload: { id: 3000 } } { route: '/users/3', payload: { id: 3000 } }

View File

@ -1,11 +0,0 @@
import { resolve } from 'path'
import { requireModule } from '../../lib/common/module'
export function loadConfig(fixture, overrides) {
const rootDir = resolve(__dirname, '../fixtures/' + fixture)
const config = requireModule(resolve(rootDir, 'nuxt.config.js'))
return Object.assign({ rootDir }, config, overrides)
}

View File

@ -1,142 +0,0 @@
import sinon from 'sinon'
let context = null
export function release() {
if (context === null) {
process.stderr.write(
'Console spy context was empty, did a previous test already release it?\n'
)
return
}
if (context.log) {
console.log = context.log // eslint-disable-line no-console
}
if (context.info) {
console.info = context.info // eslint-disable-line no-console
}
if (context.warn) {
console.warn = context.warn // eslint-disable-line no-console
}
if (context.error) {
console.error = context.error // eslint-disable-line no-console
}
if (context.stdout) {
process.stdout.write = context.stdout
}
if (context.stderr) {
process.stderr.write = context.stderr
}
context = null
delete console.spiedInTest // eslint-disable-line no-console
}
export async function intercept(levels, msg, cb) {
if (context !== null) {
process.stderr.write(
'Console spy context was not empty, did a previous test not release it?\n'
)
}
context = {}
console.spiedInTest = true // eslint-disable-line no-console
if (cb === undefined && typeof msg === 'function') {
cb = msg
msg = undefined
if (typeof levels === 'string') {
msg = levels
levels = undefined
}
}
if (cb === undefined && msg === undefined && typeof levels === 'function') {
cb = levels
levels = undefined
}
const all = levels === undefined || levels === {}
const spies = {}
if (all || levels.log) {
context.log = console.log // eslint-disable-line no-console
spies.log = console.log = sinon.spy() // eslint-disable-line no-console
}
if (all || levels.info) {
context.info = console.info // eslint-disable-line no-console
spies.info = console.info = sinon.spy() // eslint-disable-line no-console
}
if (all || levels.warn) {
context.warn = console.warn // eslint-disable-line no-console
spies.warn = console.warn = sinon.spy() // eslint-disable-line no-console
}
if (all || levels.error) {
context.error = console.error // eslint-disable-line no-console
spies.error = console.error = sinon.spy() // eslint-disable-line no-console
}
if (levels && levels.stdout) {
context.stdout = process.stdout.write
spies.stdout = process.stdout.write = sinon.spy()
}
if (levels && levels.stderr) {
context.stderr = process.stderr.write
spies.stderr = process.stderr.write = sinon.spy()
}
if (cb) {
if (msg) {
if (context.stdout) {
context.stdout(` ${msg}`)
} else {
process.stdout.write(` ${msg}`)
}
}
await cb()
release()
if (msg) {
process.stdout.write('\n')
}
}
return spies
}
export async function interceptLog(msg, cb) {
const { log } = await intercept({ log: true }, msg, cb)
return log
}
export async function interceptInfo(msg, cb) {
const { info } = await intercept({ info: true }, msg, cb)
return info
}
export async function interceptWarn(msg, cb) {
const { warn } = await intercept({ warn: true }, msg, cb)
return warn
}
export async function interceptError(msg, cb) {
const { error } = await intercept({ error: true }, msg, cb)
return error
}
export async function interceptStdout(msg, cb) {
const { stdout } = await intercept({ stdout: true }, msg, cb)
return stdout
}
export async function interceptStderr(msg, cb) {
const { stderr } = await intercept({ stderr: true }, msg, cb)
return stderr
}

View File

@ -1,28 +1,20 @@
import { normalize } from 'path' import { normalize } from 'path'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt } from '..'
import { loadFixture, getPort } from './utils'
import { Nuxt, Builder } from '..' let port
import { loadConfig } from './helpers/config'
const port = 4006
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
let builder = null // let buildSpies = null
let buildSpies = null
// Init nuxt.js and create server listening on localhost:4000
beforeAll(async () => { beforeAll(async () => {
const config = loadConfig('module', { dev: false }) const config = loadFixture('module')
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
builder = new Builder(nuxt) port = await getPort()
await builder.build()
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}, 30000) })
test('Plugin', async () => { test('Plugin', async () => {
expect(normalize(nuxt.options.plugins[0].src).includes( expect(normalize(nuxt.options.plugins[0].src).includes(
@ -42,17 +34,15 @@ test('Layout', async () => {
test('Hooks', async () => { test('Hooks', async () => {
expect(nuxt.__module_hook).toBe(1) expect(nuxt.__module_hook).toBe(1)
expect(nuxt.__renderer_hook).toBe(2) expect(nuxt.__renderer_hook).toBe(2)
expect(nuxt.__builder_hook).toBe(3)
}) })
test('Hooks - Functional', async () => { test('Hooks - Functional', async () => {
expect(nuxt.__ready_called__).toBe(true) expect(nuxt.__ready_called__).toBe(true)
expect(builder.__build_done__).toBe(true)
}) })
test('Hooks - Error', async () => { // test('Hooks - Error', async () => {
expect(buildSpies.error.calledWithMatch(/build:extendRoutes/)).toBe(true) // expect(buildSpies.error.calledWithMatch(/build:extendRoutes/)).toBe(true)
}) // })
test('Middleware', async () => { test('Middleware', async () => {
let response = await rp(url('/api')) let response = await rp(url('/api'))

View File

@ -1,5 +1,6 @@
import { resolve } from 'path' import { resolve } from 'path'
import { Nuxt, Builder } from '..' import { Nuxt, Builder } from '..'
import { loadFixture } from './utils'
describe('nuxt', () => { describe('nuxt', () => {
test('Nuxt.js Class', () => { test('Nuxt.js Class', () => {
@ -7,14 +8,15 @@ describe('nuxt', () => {
}) })
test('Nuxt.js Instance', async () => { test('Nuxt.js Instance', async () => {
const nuxt = new Nuxt({ const config = loadFixture('empty')
dev: true, const nuxt = new Nuxt(config)
rootDir: resolve(__dirname, 'fixtures', 'empty')
})
expect(typeof nuxt).toBe('object') expect(typeof nuxt).toBe('object')
expect(nuxt.options.dev).toBe(true) expect(nuxt.options.dev).toBe(false)
expect(typeof nuxt._ready.then).toBe('function') expect(typeof nuxt._ready.then).toBe('function')
await nuxt.ready() await nuxt.ready()
expect(nuxt.initialized).toBe(true) expect(nuxt.initialized).toBe(true)
}) })
@ -23,6 +25,7 @@ describe('nuxt', () => {
dev: false, dev: false,
rootDir: resolve(__dirname, 'fixtures', 'empty', 'pages') rootDir: resolve(__dirname, 'fixtures', 'empty', 'pages')
}) })
return new Builder(nuxt).build().catch(err => { return new Builder(nuxt).build().catch(err => {
let s = String(err) let s = String(err)
expect(s.includes('No `pages` directory found')).toBe(true) expect(s.includes('No `pages` directory found')).toBe(true)
@ -35,6 +38,7 @@ describe('nuxt', () => {
dev: false, dev: false,
rootDir: resolve(__dirname) rootDir: resolve(__dirname)
}) })
return new Builder(nuxt).build().catch(err => { return new Builder(nuxt).build().catch(err => {
let s = String(err) let s = String(err)
expect(s.includes("Couldn't find a `pages` directory")).toBe(true) expect(s.includes("Couldn't find a `pages` directory")).toBe(true)

View File

@ -1,27 +1,25 @@
import { Nuxt, Builder } from '..' import { Nuxt } from '..'
import { loadConfig } from './helpers/config' import { loadFixture, getPort } from './utils'
let nuxt = null let nuxt = null
const port = 4012 let port
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
const renderRoute = async _url => {
const window = await nuxt.renderAndGetWindow(url(_url))
const head = window.document.head.innerHTML
const html = window.document.body.innerHTML
return { window, head, html }
}
describe('spa', () => { describe('spa', () => {
const renderRoute = async _url => {
const window = await nuxt.renderAndGetWindow(url(_url))
const head = window.document.head.innerHTML
const html = window.document.body.innerHTML
return { window, head, html }
}
// Init nuxt.js and create server listening on localhost:4000
beforeAll(async () => { beforeAll(async () => {
const config = loadConfig('spa') const config = loadFixture('spa')
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
new Builder(nuxt).build() port = await getPort()
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}, 30000) })
test('/ (basic spa)', async () => { test('/ (basic spa)', async () => {
// const logSpy = await interceptLog() // const logSpy = await interceptLog()

View File

@ -1,9 +1,9 @@
import { Nuxt, Builder, Utils } from '..' import { Nuxt, Utils } from '..'
import { uniq } from 'lodash' import { uniq } from 'lodash'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { loadConfig } from './helpers/config' import { loadFixture, getPort } from './utils'
const port = 4008 let port
let nuxt = null let nuxt = null
// Utils // Utils
@ -15,42 +15,62 @@ const url = route => 'http://localhost:' + port + route
// const isWindows = /^win/.test(process.platform) // const isWindows = /^win/.test(process.platform)
describe('ssr', () => { // == Uniq Test ==
// Init nuxt.js and create server listening on localhost:4000 // The idea behind is pages using a shared nextId() which returns an incrementing id
beforeAll(async () => { // So all responses should strictly be different and length of unique responses should equal to responses
const config = loadConfig('ssr') // We strictly compare <foorbar>{id}</foorbar> section
// Because other response parts such as window.__NUXT may be different resulting false positive passes.
const uniqueTest = async (url) => {
let results = []
nuxt = new Nuxt(config) await Utils.parallel(range(5), async () => {
new Builder(nuxt).build() let { html } = await nuxt.renderRoute(url)
await nuxt.listen(port, 'localhost') let foobar = match(FOOBAR_REGEX, html)
}, 30000) results.push(parseInt(foobar))
})
// == Uniq Test == let isUnique = uniq(results).length === results.length
// The idea behind is pages using a shared nextId() which returns an incrementing id
// So all responses should strictly be different and length of unique responses should equal to responses
// We strictly compare <foorbar>{id}</foorbar> section
// Because other response parts such as window.__NUXT may be different resulting false positive passes.
const uniqueTest = async (t, url) => {
let results = []
await Utils.parallel(range(5), async () => { if (!isUnique) {
let { html } = await nuxt.renderRoute(url) /* eslint-disable no-console */
let foobar = match(FOOBAR_REGEX, html) console.log(url + '\n' + results.join(', ') + '\n')
results.push(parseInt(foobar))
})
let isUnique = uniq(results).length === results.length
if (!isUnique) {
/* eslint-disable no-console */
console.log(url + '\n' + results.join(', ') + '\n')
}
expect(isUnique).toBe(true)
return results
} }
expect(isUnique).toBe(true)
return results
}
// == Stress Test ==
// The idea of this test is to ensure there is no memory or data leak during SSR requests
// Or pending promises/sockets and function calls.
// Related issue: https://github.com/nuxt/nuxt.js/issues/1354
const stressTest = async (_url, concurrency = 2, steps = 4) => {port = await getPort() ; await nuxt.listen(port, 'localhost')
let statusCodes = {}
await Utils.sequence(range(steps), async () => {
await Utils.parallel(range(concurrency), async () => {
let response = await rp(url(_url), { resolveWithFullResponse: true })
// Status Code
let code = response.statusCode
if (!statusCodes[code]) {
statusCodes[code] = 0
}
statusCodes[code]++
})
})
expect(statusCodes[200]).toBe(concurrency * steps)
}
describe('ssr', () => {
beforeAll(async () => {
const config = loadFixture('ssr')
nuxt = new Nuxt(config)
port = await getPort()
await nuxt.listen(port, 'localhost')
})
test('unique responses with data()', async () => { test('unique responses with data()', async () => {
await uniqueTest('/data') await uniqueTest('/data')
}) })
@ -79,28 +99,6 @@ describe('ssr', () => {
await uniqueTest('/fetch') await uniqueTest('/fetch')
}) })
// == Stress Test ==
// The idea of this test is to ensure there is no memory or data leak during SSR requests
// Or pending promises/sockets and function calls.
// Related issue: https://github.com/nuxt/nuxt.js/issues/1354
const stressTest = async (t, _url, concurrency = 2, steps = 4) => {
let statusCodes = {}
await Utils.sequence(range(steps), async () => {
await Utils.parallel(range(concurrency), async () => {
let response = await rp(url(_url), { resolveWithFullResponse: true })
// Status Code
let code = response.statusCode
if (!statusCodes[code]) {
statusCodes[code] = 0
}
statusCodes[code]++
})
})
expect(statusCodes[200]).toBe(concurrency * steps)
}
test('stress test with asyncData', async () => { test('stress test with asyncData', async () => {
await stressTest('/asyncData') await stressTest('/asyncData')
}) })

View File

@ -14,7 +14,7 @@ export async function start(options = {}) {
) )
} }
export async function stop() { export async function close() {
if (!browser) return if (!browser) return
await browser.close() await browser.close()
} }

21
test/utils/index.js Normal file
View File

@ -0,0 +1,21 @@
import path from 'path'
import fs from 'fs'
import _getPort from 'get-port'
import { requireModule } from '../../lib/common/module'
export function loadFixture(fixture, overrides) {
const rootDir = path.resolve(__dirname, '../fixtures/' + fixture)
const configFile = path.resolve(rootDir, 'nuxt.config.js')
const config = fs.existsSync(configFile) ? requireModule(configFile) : {}
config.rootDir = rootDir
config.dev = false
return Object.assign({}, config, overrides)
}
export function getPort() {
return _getPort()
}

View File

@ -1,4 +1,3 @@
// eslint-disable // eslint-disable
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000 jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000
jasmine.stopSpecOnExpectationFailure = true

View File

@ -1,30 +1,19 @@
import { resolve } from 'path'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import { Nuxt } from '..'
import { loadFixture, getPort } from './utils'
import { Nuxt, Builder } from '..' let port
import styleLoader from '../lib/builder/webpack/style-loader'
import { loadConfig } from './helpers/config'
const port = 4007
const url = route => 'http://localhost:' + port + route const url = route => 'http://localhost:' + port + route
let nuxt = null let nuxt = null
let builder = null
describe('with-config', () => { describe('with-config', () => {
// Init nuxt.js and create server listening on localhost:4000
beforeAll(async () => { beforeAll(async () => {
const config = loadConfig('with-config', { const config = loadFixture('with-config')
dev: false
})
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
builder = new Builder(nuxt) port = await getPort()
await builder.build()
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}, 30000) })
test('/', async () => { test('/', async () => {
// const logSpy = await interceptLog() // const logSpy = await interceptLog()
@ -181,14 +170,6 @@ describe('with-config', () => {
expect(html.includes('<h1>About page</h1>')).toBe(true) expect(html.includes('<h1>About page</h1>')).toBe(true)
}) })
test('Check stats.json generated by build.analyze', () => {
const stats = require(resolve(
__dirname,
'fixtures/with-config/.nuxt/dist/stats.json'
))
expect(stats.assets.length > 0).toBe(true)
})
test('Check /test/test.txt with custom serve-static options', async () => { test('Check /test/test.txt with custom serve-static options', async () => {
const { headers } = await rp(url('/test/test.txt'), { const { headers } = await rp(url('/test/test.txt'), {
resolveWithFullResponse: true resolveWithFullResponse: true
@ -201,15 +182,6 @@ describe('with-config', () => {
.rejects.toMatchObject({ statusCode: 404 }) .rejects.toMatchObject({ statusCode: 404 })
}) })
test('Check build.styleResources for style-resources-loader', async () => {
const loaders = styleLoader.call(builder, 'scss')
const loader = loaders.find(l => l.loader === 'style-resources-loader')
expect(typeof loader).toBe('object')
expect(loader.options).toEqual({
patterns: ['~/assets/pre-process.scss']
})
})
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test('Closing server and nuxt.js', async () => { test('Closing server and nuxt.js', async () => {
await nuxt.close() await nuxt.close()

108
yarn.lock
View File

@ -1750,6 +1750,15 @@ cosmiconfig@^2.1.0, cosmiconfig@^2.1.1:
parse-json "^2.2.0" parse-json "^2.2.0"
require-from-string "^1.1.0" require-from-string "^1.1.0"
cosmiconfig@^3.0.1:
version "3.1.0"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-3.1.0.tgz#640a94bf9847f321800403cd273af60665c73397"
dependencies:
is-directory "^0.3.1"
js-yaml "^3.9.0"
parse-json "^3.0.0"
require-from-string "^2.0.1"
create-ecdh@^4.0.0: create-ecdh@^4.0.0:
version "4.0.0" version "4.0.0"
resolved "https://registry.yarnpkg.com/create-ecdh/-/create-ecdh-4.0.0.tgz#888c723596cdf7612f6498233eebd7a35301737d" resolved "https://registry.yarnpkg.com/create-ecdh/-/create-ecdh-4.0.0.tgz#888c723596cdf7612f6498233eebd7a35301737d"
@ -2263,13 +2272,13 @@ entities@^1.1.1, entities@~1.1.1:
version "1.1.1" version "1.1.1"
resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.1.tgz#6e5c2d0a5621b5dadaecef80b90edfb5cd7772f0" resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.1.tgz#6e5c2d0a5621b5dadaecef80b90edfb5cd7772f0"
errno@^0.1.3, errno@~0.1.7: errno@^0.1.3, errno@^0.1.4, errno@~0.1.7:
version "0.1.7" version "0.1.7"
resolved "https://registry.yarnpkg.com/errno/-/errno-0.1.7.tgz#4684d71779ad39af177e3f007996f7c67c852618" resolved "https://registry.yarnpkg.com/errno/-/errno-0.1.7.tgz#4684d71779ad39af177e3f007996f7c67c852618"
dependencies: dependencies:
prr "~1.0.1" prr "~1.0.1"
error-ex@^1.2.0: error-ex@^1.2.0, error-ex@^1.3.1:
version "1.3.1" version "1.3.1"
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.1.tgz#f855a86ce61adc4e8621c3cda21e7a7612c3a8dc" resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.1.tgz#f855a86ce61adc4e8621c3cda21e7a7612c3a8dc"
dependencies: dependencies:
@ -2454,11 +2463,54 @@ eslint@^4.18.2:
table "4.0.2" table "4.0.2"
text-table "~0.2.0" text-table "~0.2.0"
eslint@^4.5.0:
version "4.19.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-4.19.0.tgz#9e900efb5506812ac374557034ef6f5c3642fc4c"
dependencies:
ajv "^5.3.0"
babel-code-frame "^6.22.0"
chalk "^2.1.0"
concat-stream "^1.6.0"
cross-spawn "^5.1.0"
debug "^3.1.0"
doctrine "^2.1.0"
eslint-scope "^3.7.1"
eslint-visitor-keys "^1.0.0"
espree "^3.5.4"
esquery "^1.0.0"
esutils "^2.0.2"
file-entry-cache "^2.0.0"
functional-red-black-tree "^1.0.1"
glob "^7.1.2"
globals "^11.0.1"
ignore "^3.3.3"
imurmurhash "^0.1.4"
inquirer "^3.0.6"
is-resolvable "^1.0.0"
js-yaml "^3.9.1"
json-stable-stringify-without-jsonify "^1.0.1"
levn "^0.3.0"
lodash "^4.17.4"
minimatch "^3.0.2"
mkdirp "^0.5.1"
natural-compare "^1.4.0"
optionator "^0.8.2"
path-is-inside "^1.0.2"
pluralize "^7.0.0"
progress "^2.0.0"
regexpp "^1.0.1"
require-uncached "^1.0.3"
semver "^5.3.0"
strip-ansi "^4.0.0"
strip-json-comments "~2.0.1"
table "4.0.2"
text-table "~0.2.0"
esm@^3.0.6: esm@^3.0.6:
version "3.0.6" version "3.0.6"
resolved "https://registry.yarnpkg.com/esm/-/esm-3.0.6.tgz#0b327fb312e287d9e1a95e728c18b79603fb6d9e" resolved "https://registry.yarnpkg.com/esm/-/esm-3.0.6.tgz#0b327fb312e287d9e1a95e728c18b79603fb6d9e"
espree@^3.5.2: espree@^3.5.2, espree@^3.5.4:
version "3.5.4" version "3.5.4"
resolved "https://registry.yarnpkg.com/espree/-/espree-3.5.4.tgz#b0f447187c8a8bed944b815a660bddf5deb5d1a7" resolved "https://registry.yarnpkg.com/espree/-/espree-3.5.4.tgz#b0f447187c8a8bed944b815a660bddf5deb5d1a7"
dependencies: dependencies:
@ -2958,6 +3010,10 @@ get-caller-file@^1.0.1:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.2.tgz#f702e63127e7e231c160a80c1554acb70d5047e5" resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.2.tgz#f702e63127e7e231c160a80c1554acb70d5047e5"
get-port@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/get-port/-/get-port-3.2.0.tgz#dd7ce7de187c06c8bf353796ac71e099f0980ebc"
get-stream@^3.0.0: get-stream@^3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14" resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-3.0.0.tgz#8e943d1358dc37555054ecbe2edb05aa174ede14"
@ -3951,6 +4007,17 @@ jest-resolve@^22.4.2:
browser-resolve "^1.11.2" browser-resolve "^1.11.2"
chalk "^2.0.1" chalk "^2.0.1"
jest-runner-eslint@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/jest-runner-eslint/-/jest-runner-eslint-0.4.0.tgz#938a54fb767d1803d45613ae3eac0adc2de426db"
dependencies:
cosmiconfig "^3.0.1"
eslint "^4.5.0"
find-up "^2.1.0"
pify "3.0.0"
throat "4.1.0"
worker-farm "1.5.0"
jest-runner@^22.4.2: jest-runner@^22.4.2:
version "22.4.2" version "22.4.2"
resolved "https://registry.yarnpkg.com/jest-runner/-/jest-runner-22.4.2.tgz#19390ea9d99f768973e16f95a1efa351c0017e87" resolved "https://registry.yarnpkg.com/jest-runner/-/jest-runner-22.4.2.tgz#19390ea9d99f768973e16f95a1efa351c0017e87"
@ -4050,7 +4117,7 @@ js-tokens@^3.0.0, js-tokens@^3.0.2:
version "3.0.2" version "3.0.2"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
js-yaml@^3.4.3, js-yaml@^3.7.0, js-yaml@^3.9.1: js-yaml@^3.4.3, js-yaml@^3.7.0, js-yaml@^3.9.0, js-yaml@^3.9.1:
version "3.11.0" version "3.11.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.11.0.tgz#597c1a8bd57152f26d622ce4117851a51f5ebaef" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.11.0.tgz#597c1a8bd57152f26d622ce4117851a51f5ebaef"
dependencies: dependencies:
@ -4989,6 +5056,12 @@ parse-json@^2.2.0:
dependencies: dependencies:
error-ex "^1.2.0" error-ex "^1.2.0"
parse-json@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/parse-json/-/parse-json-3.0.0.tgz#fa6f47b18e23826ead32f263e744d0e1e847fb13"
dependencies:
error-ex "^1.3.1"
parse5@4.0.0: parse5@4.0.0:
version "4.0.0" version "4.0.0"
resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608" resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608"
@ -5081,14 +5154,14 @@ performance-now@^2.1.0:
version "2.1.0" version "2.1.0"
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
pify@3.0.0, pify@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176"
pify@^2.0.0, pify@^2.3.0: pify@^2.0.0, pify@^2.3.0:
version "2.3.0" version "2.3.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c" resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
pify@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176"
pinkie-promise@^2.0.0: pinkie-promise@^2.0.0:
version "2.0.1" version "2.0.1"
resolved "https://registry.yarnpkg.com/pinkie-promise/-/pinkie-promise-2.0.1.tgz#2135d6dfa7a358c069ac9b178776288228450ffa" resolved "https://registry.yarnpkg.com/pinkie-promise/-/pinkie-promise-2.0.1.tgz#2135d6dfa7a358c069ac9b178776288228450ffa"
@ -5999,6 +6072,10 @@ regex-not@^1.0.0, regex-not@^1.0.2:
extend-shallow "^3.0.2" extend-shallow "^3.0.2"
safe-regex "^1.1.0" safe-regex "^1.1.0"
regexpp@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/regexpp/-/regexpp-1.0.1.tgz#d857c3a741dce075c2848dcb019a0a975b190d43"
regexpu-core@^1.0.0: regexpu-core@^1.0.0:
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/regexpu-core/-/regexpu-core-1.0.0.tgz#86a763f58ee4d7c2f6b102e4764050de7ed90c6b" resolved "https://registry.yarnpkg.com/regexpu-core/-/regexpu-core-1.0.0.tgz#86a763f58ee4d7c2f6b102e4764050de7ed90c6b"
@ -6133,6 +6210,10 @@ require-from-string@^1.1.0:
version "1.2.1" version "1.2.1"
resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-1.2.1.tgz#529c9ccef27380adfec9a2f965b649bbee636418" resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-1.2.1.tgz#529c9ccef27380adfec9a2f965b649bbee636418"
require-from-string@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.1.tgz#c545233e9d7da6616e9d59adfb39fc9f588676ff"
require-main-filename@^1.0.1: require-main-filename@^1.0.1:
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1" resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1"
@ -6799,7 +6880,7 @@ text-table@^0.2.0, text-table@~0.2.0:
version "0.2.0" version "0.2.0"
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4" resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
throat@^4.0.0: throat@4.1.0, throat@^4.0.0:
version "4.1.0" version "4.1.0"
resolved "https://registry.yarnpkg.com/throat/-/throat-4.1.0.tgz#89037cbc92c56ab18926e6ba4cbb200e15672a6a" resolved "https://registry.yarnpkg.com/throat/-/throat-4.1.0.tgz#89037cbc92c56ab18926e6ba4cbb200e15672a6a"
@ -7401,6 +7482,13 @@ wordwrap@~0.0.2:
version "0.0.3" version "0.0.3"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107" resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107"
worker-farm@1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/worker-farm/-/worker-farm-1.5.0.tgz#adfdf0cd40581465ed0a1f648f9735722afd5c8d"
dependencies:
errno "^0.1.4"
xtend "^4.0.1"
worker-farm@^1.5.2: worker-farm@^1.5.2:
version "1.6.0" version "1.6.0"
resolved "https://registry.yarnpkg.com/worker-farm/-/worker-farm-1.6.0.tgz#aecc405976fab5a95526180846f0dba288f3a4a0" resolved "https://registry.yarnpkg.com/worker-farm/-/worker-farm-1.6.0.tgz#aecc405976fab5a95526180846f0dba288f3a4a0"
@ -7455,7 +7543,7 @@ xml-name-validator@^3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a" resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a"
xtend@^4.0.0, xtend@~4.0.1: xtend@^4.0.0, xtend@^4.0.1, xtend@~4.0.1:
version "4.0.1" version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af" resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"