chore: test utils cleanup (#4180)

This commit is contained in:
Jonas Galvez 2018-10-24 10:53:34 -03:00 committed by Pooya Parsa
parent fae4df3377
commit 5ec5cda1f8
6 changed files with 47 additions and 54 deletions

View File

@ -1,5 +1,5 @@
import Browser from '../utils/browser' import Browser from '../utils/browser'
import { loadFixture, getPort, Nuxt, Utils } from '../utils' import { loadFixture, getPort, Nuxt, waitFor } from '../utils'
let port let port
const browser = new Browser() const browser = new Browser()
@ -105,7 +105,7 @@ describe('children patch (browser)', () => {
await page.type('[data-test-search-input]', 'gu') await page.type('[data-test-search-input]', 'gu')
await Utils.waitFor(250) await waitFor(250)
const newCountries = await page.$$text('[data-test-search-result]') const newCountries = await page.$$text('[data-test-search-result]')
expect(newCountries.length).toBe(1) expect(newCountries.length).toBe(1)
expect(newCountries).toEqual(['Guinea']) expect(newCountries).toEqual(['Guinea'])

View File

@ -1,7 +1,7 @@
import { resolve, join } from 'path' import { resolve, join } from 'path'
import { spawn } from 'cross-spawn' import { spawn } from 'cross-spawn'
import { writeFileSync } from 'fs-extra' import { writeFileSync } from 'fs-extra'
import { getPort, rp, waitUntil, Utils } from '../utils' import { getPort, rp, waitUntil, waitFor } from '../utils'
let port let port
const rootDir = resolve(__dirname, '..', 'fixtures/cli') const rootDir = resolve(__dirname, '..', 'fixtures/cli')
@ -41,7 +41,7 @@ describe.skip.appveyor('cli', () => {
writeFileSync(serverMiddlewarePath, '// This file is used to test custom chokidar watchers.\n') writeFileSync(serverMiddlewarePath, '// This file is used to test custom chokidar watchers.\n')
// Wait 2s for picking up changes // Wait 2s for picking up changes
await Utils.waitFor(2000) await waitFor(2000)
// [Add actual test for changes here] // [Add actual test for changes here]

View File

@ -1,5 +1,6 @@
import { uniq } from 'lodash' import { uniq } from 'lodash'
import { loadFixture, getPort, Nuxt, Utils, rp } from '../utils' import { loadFixture, getPort, Nuxt, rp, sequence, parallel } from '../utils'
let port let port
let nuxt = null let nuxt = null
@ -21,7 +22,7 @@ const url = route => 'http://localhost:' + port + route
const uniqueTest = async (url) => { const uniqueTest = async (url) => {
const results = [] const results = []
await Utils.parallel(range(5), async () => { await parallel(range(5), async () => {
const { html } = await nuxt.renderRoute(url) const { html } = await nuxt.renderRoute(url)
const foobar = match(FOOBAR_REGEX, html) const foobar = match(FOOBAR_REGEX, html)
results.push(parseInt(foobar)) results.push(parseInt(foobar))
@ -46,8 +47,8 @@ const uniqueTest = async (url) => {
const stressTest = async (_url, concurrency = 2, steps = 4) => { const stressTest = async (_url, concurrency = 2, steps = 4) => {
const statusCodes = {} const statusCodes = {}
await Utils.sequence(range(steps), async () => { await sequence(range(steps), async () => {
await Utils.parallel(range(concurrency), async () => { await parallel(range(concurrency), async () => {
const response = await rp(url(_url), { resolveWithFullResponse: true }) const response = await rp(url(_url), { resolveWithFullResponse: true })
// Status Code // Status Code
const code = response.statusCode const code = response.statusCode

View File

@ -1,5 +1,6 @@
import path from 'path' import path from 'path'
import { Utils, waitUntil } from '../utils' import { waitUntil } from '../utils'
import * as Utils from '../../packages/common/src/index'
describe('utils', () => { describe('utils', () => {
test('encodeHtml', () => { test('encodeHtml', () => {

View File

@ -1,54 +1,20 @@
import path from 'path'
import fs from 'fs'
import klawSync from 'klaw-sync' import klawSync from 'klaw-sync'
import { waitFor } from '../../packages/common/src/utils'
export { default as getPort } from 'get-port'
export { default as rp } from 'request-promise-native'
import _getPort from 'get-port' export * from './nuxt'
import { defaultsDeep, find } from 'lodash'
import _rp from 'request-promise-native'
import corePkg from '../../packages/core/package.json'
import * as _Utils from '../../packages/common/src/index' // Pauses execution for a determined amount of time (`duration`)
// until `condition` is met. Also allows specifying the `interval`
export { Nuxt } from '../../packages/core/src/index' // at which the condition is checked during the waiting period.
export { Builder, Generator } from '../../packages/builder/src/index'
export const rp = _rp
export const getPort = _getPort
export const version = corePkg.version
export const Utils = _Utils
export const Options = _Utils.Options
export const loadFixture = async function (fixture, overrides) {
const rootDir = path.resolve(__dirname, '..', 'fixtures', fixture)
const configFile = path.resolve(rootDir, 'nuxt.config.js')
let config = fs.existsSync(configFile) ? (await import(`../fixtures/${fixture}/nuxt.config`)).default : {}
if (typeof config === 'function') {
config = await config()
}
config.rootDir = rootDir
config.dev = false
config.test = true
return defaultsDeep({}, overrides, config)
}
/**
* Prepare an object to pass to the createSportsSelectionView function
* @param {Function} condition return true to stop the waiting
* @param {Number} duration seconds totally wait
* @param {Number} interval milliseconds interval to check the condition
*
* @returns {Boolean} true: timeout, false: condition becomes true within total time
*/
export const waitUntil = async function waitUntil(condition, duration = 20, interval = 250) { export const waitUntil = async function waitUntil(condition, duration = 20, interval = 250) {
let iterator = 0 let iterator = 0
const steps = Math.floor(duration * 1000 / interval) const steps = Math.floor(duration * 1000 / interval)
while (!condition() && iterator < steps) { while (!condition() && iterator < steps) {
await Utils.waitFor(interval) await waitFor(interval)
iterator++ iterator++
} }
@ -60,10 +26,10 @@ export const waitUntil = async function waitUntil(condition, duration = 20, inte
export const listPaths = function listPaths(dir, pathsBefore = [], options = {}) { export const listPaths = function listPaths(dir, pathsBefore = [], options = {}) {
if (Array.isArray(pathsBefore) && pathsBefore.length) { if (Array.isArray(pathsBefore) && pathsBefore.length) {
// only return files that didn't exist before building // Only return files that didn't exist before building
// and files that have been changed // and files that have been changed
options.filter = (item) => { options.filter = (item) => {
const foundItem = find(pathsBefore, (itemBefore) => { const foundItem = pathsBefore.find((itemBefore) => {
return item.path === itemBefore.path return item.path === itemBefore.path
}) })
return typeof foundItem === 'undefined' || return typeof foundItem === 'undefined' ||

25
test/utils/nuxt.js Normal file
View File

@ -0,0 +1,25 @@
import path from 'path'
import fs from 'fs'
import { defaultsDeep } from 'lodash'
export { version } from '../../packages/core/package.json'
export { Nuxt } from '../../packages/core/src/index'
export { Builder, Generator } from '../../packages/builder/src/index'
export * from '../../packages/common/src/index'
export const loadFixture = async function (fixture, overrides) {
const rootDir = path.resolve(__dirname, '..', 'fixtures', fixture)
const configFile = path.resolve(rootDir, 'nuxt.config.js')
let config = fs.existsSync(configFile) ? (await import(`../fixtures/${fixture}/nuxt.config`)).default : {}
if (typeof config === 'function') {
config = await config()
}
config.rootDir = rootDir
config.dev = false
config.test = true
return defaultsDeep({}, overrides, config)
}