Skip to content

Commit

Permalink
Made jest tests less flaky - increase timeout, shared async exec, bro…
Browse files Browse the repository at this point in the history
…ught back fileHelpers tests.
  • Loading branch information
nataliecarey committed Sep 28, 2022
1 parent 8de4e2a commit 4cad840
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 75 deletions.
6 changes: 3 additions & 3 deletions __tests__/spec/install.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-env jest */

const child_process = require('child_process') // eslint-disable-line camelcase
const fs = require('fs')
const path = require('path')

const utils = require('../util')
const { exec } = require('../../lib/exec')

describe('npm install', () => {
const tmpDir = utils.mkdtempSync()
Expand All @@ -13,9 +13,9 @@ describe('npm install', () => {
const testDir = path.join(tmpDir, 'install-no-optional')
await utils.mkPrototype(testDir)

child_process.execSync(
await exec(
'npm install',
{ cwd: testDir, env: { LANG: process.env.LANG, PATH: process.env.PATH } }
{ cwd: testDir }
)

expect(
Expand Down
4 changes: 3 additions & 1 deletion __tests__/spec/sanity-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const { generateAssetsSync } = require('../../lib/build/tasks')
const fse = require('fs-extra')
const { projectDir } = require('../../lib/path-utils')

const createKitTimeout = parseInt(process.env.CREATE_KIT_TIMEOUT || '90000', 10)

function readFile (pathFromRoot) {
return fs.readFileSync(path.join(__dirname, '../../' + pathFromRoot), 'utf8')
}
Expand All @@ -29,7 +31,7 @@ describe('The Prototype Kit', () => {
jest.spyOn(fse, 'writeFileSync').mockImplementation(() => {})
jest.spyOn(sass, 'compile').mockImplementation((css, options) => ({ css }))
generateAssetsSync()
})
}, createKitTimeout)

it('should call writeFileSync with result css from sass.compile', () => {
expect(fse.writeFileSync).toHaveBeenCalledWith(
Expand Down
40 changes: 22 additions & 18 deletions __tests__/util/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const child_process = require('child_process') // eslint-disable-line camelcase

const fs = require('fs-extra')
const os = require('os')
const path = require('path')

const fs = require('fs-extra')

const { exec } = require('../../lib/exec')

/**
* An ID that will be shared between all process in the same Jest test run,
* this is useful for sharing fixture files. Normally sharing state across Jest
Expand Down Expand Up @@ -71,6 +72,8 @@ async function mkPrototype (prototypePath, {
}
}

const startTime = Date.now()

try {
// Remove previous test starter project
await fs.remove(prototypePath)
Expand All @@ -79,20 +82,21 @@ async function mkPrototype (prototypePath, {
await fs.mkdirp(prototypePath)

// Generate starter project and start
child_process.execSync(
'npm i -g',
{ stdio: 'inherit' }
await exec(
'npm i -g'
)

// Generate starter project and start
child_process.execSync(
await exec(
'govuk-prototype-kit create --version local',
{ cwd: prototypePath, env: { ...process.env, env: 'test' }, stdio: 'inherit' }
{ cwd: prototypePath, env: { ...process.env, env: 'test' } }
)

if (allowTracking !== undefined) {
await fs.writeFile(path.join(prototypePath, 'usage-data-config.json'), `{ "collectUsageData": ${allowTracking}}`)
await fs.writeFile(path.join(prototypePath, 'usage-data-config.json'), `{ "collectUsageData": ${!!allowTracking}}`)
}

console.log(`Kit creation took [${Math.round((Date.now() - startTime) / 100) / 10}] seconds`)
} catch (error) {
console.error(error.message)
console.error(error.stack)
Expand All @@ -102,28 +106,28 @@ async function mkPrototype (prototypePath, {
}
}

function installExtensions (prototypePath, extensionNames) {
async function installExtensions (prototypePath, extensionNames) {
let extensionNamesProcessed = extensionNames || []
if (!Array.isArray(extensionNames)) {
extensionNamesProcessed = [extensionNames]
}
child_process.execSync(
return exec(
`npm install ${extensionNamesProcessed.join(' ')}`,
{ cwd: prototypePath, env: { ...process.env, env: 'test' }, stdio: 'inherit' }
{ cwd: prototypePath, env: { ...process.env, env: 'test' } }
)
}

function npmInstall (pathToRunInstallIn) {
return child_process.exec(
async function npmInstall (pathToRunInstallIn) {
return exec(
'npm install',
{ cwd: pathToRunInstallIn, env: { ...process.env, env: 'test' }, stdio: 'inherit' }
{ cwd: pathToRunInstallIn, env: { ...process.env, env: 'test' } }
)
}

function startPrototype (prototypePath) {
child_process.execSync(
async function startPrototype (prototypePath) {
return exec(
'npm start',
{ cwd: prototypePath, env: { ...process.env, env: 'test' }, stdio: 'inherit' }
{ cwd: prototypePath, env: { ...process.env, env: 'test' } }
)
}

Expand Down
1 change: 0 additions & 1 deletion cypress/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)
// const spawn = require('child_process').spawn
const fs = require('fs')
const fsp = fs.promises
const path = require('path')
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/prototype-admin-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const path = require('path')
const extensions = require('../extensions/extensions')
const config = require('../config').getConfig()
const { requestHttpsJson } = require('../utils')
const { exec } = require('../upgradeToV13/exec')
const { exec } = require('../exec')

const appViews = extensions.getAppViews([
path.join(projectDir, 'node_modules'),
Expand Down
48 changes: 0 additions & 48 deletions lib/upgradeToV13/exec.js

This file was deleted.

2 changes: 1 addition & 1 deletion lib/upgradeToV13/fileHelpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { mockFileSystem } = require('../../__tests__/util/mockFileSystem')
const { projectDir } = require('../path-utils')
const { removeLineFromFile, replaceStartOfFile, deleteFile, deleteDirectory } = require('./fileHelpers')

describe.skip('file helpers', () => {
describe('file helpers', () => {
const joinLines = arr => arr.join(os.EOL)
const writeFileToMockFile = (filePathParts, contentStringOrArray) => testScope.fileSystem
.writeFile(filePathParts, typeof contentStringOrArray === 'string' ? contentStringOrArray : joinLines(contentStringOrArray))
Expand Down
2 changes: 1 addition & 1 deletion lib/upgradeToV13/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
} = require('./fileHelpers')
const path = require('path')
const { projectDir, packageDir } = require('../path-utils')
const { exec } = require('./exec')
const { exec } = require('../exec')

const fullReport = []
const reportSuccess = tag => fullReport.push(() => console.log(c.green(`Succeeded [${tag}]`)))
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"test:acceptance:open": "KIT_TEST_DIR=tmp/test-prototype-package start-server-and-test 'node cypress/scripts/run-starter-prototype' 3000 'cypress open'",
"test:smoke": "cypress run --spec \"cypress/integration/0-smoke-tests/*\"",
"test:unit": "jest --detectOpenHandles lib bin",
"test:integration": "IS_INTEGRATION_TEST=true jest --detectOpenHandles --testTimeout=30000 __tests__",
"test:integration": "CREATE_KIT_TIMEOUT=90000 IS_INTEGRATION_TEST=true jest --detectOpenHandles --testTimeout=60000 __tests__",
"test": "npm run test:unit && npm run test:integration && npm run lint"
},
"dependencies": {
Expand Down

0 comments on commit 4cad840

Please sign in to comment.