mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-27 05:42:36 +00:00
chore: test utils cleanup (#4180)
This commit is contained in:
parent
fae4df3377
commit
5ec5cda1f8
@ -1,5 +1,5 @@
|
||||
import Browser from '../utils/browser'
|
||||
import { loadFixture, getPort, Nuxt, Utils } from '../utils'
|
||||
import { loadFixture, getPort, Nuxt, waitFor } from '../utils'
|
||||
|
||||
let port
|
||||
const browser = new Browser()
|
||||
@ -105,7 +105,7 @@ describe('children patch (browser)', () => {
|
||||
|
||||
await page.type('[data-test-search-input]', 'gu')
|
||||
|
||||
await Utils.waitFor(250)
|
||||
await waitFor(250)
|
||||
const newCountries = await page.$$text('[data-test-search-result]')
|
||||
expect(newCountries.length).toBe(1)
|
||||
expect(newCountries).toEqual(['Guinea'])
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { resolve, join } from 'path'
|
||||
import { spawn } from 'cross-spawn'
|
||||
import { writeFileSync } from 'fs-extra'
|
||||
import { getPort, rp, waitUntil, Utils } from '../utils'
|
||||
import { getPort, rp, waitUntil, waitFor } from '../utils'
|
||||
|
||||
let port
|
||||
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')
|
||||
|
||||
// Wait 2s for picking up changes
|
||||
await Utils.waitFor(2000)
|
||||
await waitFor(2000)
|
||||
|
||||
// [Add actual test for changes here]
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
|
||||
import { uniq } from 'lodash'
|
||||
import { loadFixture, getPort, Nuxt, Utils, rp } from '../utils'
|
||||
import { loadFixture, getPort, Nuxt, rp, sequence, parallel } from '../utils'
|
||||
|
||||
let port
|
||||
let nuxt = null
|
||||
@ -21,7 +22,7 @@ const url = route => 'http://localhost:' + port + route
|
||||
const uniqueTest = async (url) => {
|
||||
const results = []
|
||||
|
||||
await Utils.parallel(range(5), async () => {
|
||||
await parallel(range(5), async () => {
|
||||
const { html } = await nuxt.renderRoute(url)
|
||||
const foobar = match(FOOBAR_REGEX, html)
|
||||
results.push(parseInt(foobar))
|
||||
@ -46,8 +47,8 @@ const uniqueTest = async (url) => {
|
||||
const stressTest = async (_url, concurrency = 2, steps = 4) => {
|
||||
const statusCodes = {}
|
||||
|
||||
await Utils.sequence(range(steps), async () => {
|
||||
await Utils.parallel(range(concurrency), async () => {
|
||||
await sequence(range(steps), async () => {
|
||||
await parallel(range(concurrency), async () => {
|
||||
const response = await rp(url(_url), { resolveWithFullResponse: true })
|
||||
// Status Code
|
||||
const code = response.statusCode
|
||||
|
@ -1,5 +1,6 @@
|
||||
import path from 'path'
|
||||
import { Utils, waitUntil } from '../utils'
|
||||
import { waitUntil } from '../utils'
|
||||
import * as Utils from '../../packages/common/src/index'
|
||||
|
||||
describe('utils', () => {
|
||||
test('encodeHtml', () => {
|
||||
|
@ -1,54 +1,20 @@
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
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'
|
||||
import { defaultsDeep, find } from 'lodash'
|
||||
import _rp from 'request-promise-native'
|
||||
import corePkg from '../../packages/core/package.json'
|
||||
export * from './nuxt'
|
||||
|
||||
import * as _Utils from '../../packages/common/src/index'
|
||||
|
||||
export { Nuxt } from '../../packages/core/src/index'
|
||||
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
|
||||
*/
|
||||
// Pauses execution for a determined amount of time (`duration`)
|
||||
// until `condition` is met. Also allows specifying the `interval`
|
||||
// at which the condition is checked during the waiting period.
|
||||
export const waitUntil = async function waitUntil(condition, duration = 20, interval = 250) {
|
||||
let iterator = 0
|
||||
const steps = Math.floor(duration * 1000 / interval)
|
||||
|
||||
while (!condition() && iterator < steps) {
|
||||
await Utils.waitFor(interval)
|
||||
await waitFor(interval)
|
||||
iterator++
|
||||
}
|
||||
|
||||
@ -60,10 +26,10 @@ export const waitUntil = async function waitUntil(condition, duration = 20, inte
|
||||
|
||||
export const listPaths = function listPaths(dir, pathsBefore = [], options = {}) {
|
||||
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
|
||||
options.filter = (item) => {
|
||||
const foundItem = find(pathsBefore, (itemBefore) => {
|
||||
const foundItem = pathsBefore.find((itemBefore) => {
|
||||
return item.path === itemBefore.path
|
||||
})
|
||||
return typeof foundItem === 'undefined' ||
|
||||
|
25
test/utils/nuxt.js
Normal file
25
test/utils/nuxt.js
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user