Skip to content

Commit

Permalink
test: avoid file side-effects in unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
iiroj committed Dec 2, 2023
1 parent dc0fe71 commit 4844150
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 73 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage
test/unit/__mocks__
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/unit/__mocks__
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'

import { normalizePath } from '../../../lib/normalizePath.js'
import { normalizePath } from '../../lib/normalizePath.js'

/**
* Create temporary random directory and return its path
Expand Down
2 changes: 1 addition & 1 deletion test/integration/__utils__/withGitIntegration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import makeConsoleMock from 'consolemock'

import { execGit as execGitBase } from '../../../lib/execGit.js'
import lintStaged from '../../../lib/index.js'
import { createTempDir } from '../../__utils__/createTempDir.js'

import { createTempDir } from './createTempDir.js'
import { isWindowsActions } from './isWindows'
import { normalizeWindowsNewlines } from './normalizeWindowsNewlines.js'

Expand Down
2 changes: 1 addition & 1 deletion test/integration/not-inside-git-repo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { jest } from '@jest/globals'
import makeConsoleMock from 'consolemock'

import lintStaged from '../../lib/index.js'
import { createTempDir } from '../__utils__/createTempDir.js'

import { createTempDir } from './__utils__/createTempDir.js'
import { prettierWrite } from './__fixtures__/configs.js'

jest.setTimeout(20000)
Expand Down
1 change: 1 addition & 0 deletions test/unit/__mocks__/invalid-json-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{
1 change: 1 addition & 0 deletions test/unit/__mocks__/invalid-yml-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{
2 changes: 0 additions & 2 deletions test/unit/__mocks__/package.yml

This file was deleted.

161 changes: 93 additions & 68 deletions test/unit/loadConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'node:path'

import makeConsoleMock from 'consolemock'

import { createTempDir } from '../__utils__/createTempDir.js'
import { loadConfig } from '../../lib/loadConfig.js'

/** Unfortunately necessary due to non-ESM tests. */
Expand Down Expand Up @@ -64,15 +65,12 @@ describe('loadConfig', () => {
it('should return null config when YAML config file is invalid', async () => {
expect.assertions(1)

const configFile = path.join(__dirname, '__mocks__', 'lint-staged.yml')

await fs.writeFile(configFile, '{')

const { config } = await loadConfig({ configPath: configFile }, logger)
const { config } = await loadConfig(
{ configPath: path.join(__dirname, '__mocks__', 'invalid-config-file.yml') },
logger
)

expect(config).toBeUndefined()

await fs.rm(configFile)
})

it('should load CommonJS config file from absolute path', async () => {
Expand Down Expand Up @@ -196,98 +194,125 @@ describe('loadConfig', () => {
it('should return empty object ".lintstagedrc.json" file is invalid', async () => {
expect.assertions(1)

const configFile = path.join(__dirname, '__mocks__', '.lintstagedrc.json')

await fs.writeFile(configFile, '{')

const result = await loadConfig({ configPath: configFile }, logger)
const result = await loadConfig(
{ configPath: path.join(__dirname, '__mocks__', 'invalid-json-config.json') },
logger
)

expect(result).toMatchInlineSnapshot(`{}`)

await fs.rm(configFile)
})

it('should read config from package.json', async () => {
expect.assertions(1)
const tempDir = await createTempDir()
const configPath = path.join(tempDir, 'package.json')

const configFile = path.join(__dirname, '__mocks__', 'package.json')
try {
expect.assertions(1)

await fs.writeFile(
configFile,
JSON.stringify({
'lint-staged': {
'*': 'mytask',
},
})
)
await fs.writeFile(
configPath,
JSON.stringify({
'lint-staged': {
'*': 'mytask',
},
})
)

const { config } = await loadConfig({ configPath: configFile }, logger)
const { config } = await loadConfig({ configPath }, logger)

expect(config).toMatchInlineSnapshot(`
{
"*": "mytask",
}
expect(config).toMatchInlineSnapshot(`
{
"*": "mytask",
}
`)
await fs.rm(configFile)
} finally {
await fs.rm(tempDir, { recursive: true })
}
})

it('should return null config when package.json file is invalid', async () => {
expect.assertions(1)

const configFile = path.join(__dirname, '__mocks__', 'package.json')
const tempDir = await createTempDir()
const configPath = path.join(tempDir, 'package.json')

await fs.writeFile(configFile, '{')
try {
expect.assertions(1)

const { config } = await loadConfig({ configPath: configFile }, logger)
await fs.writeFile(configPath, '{')

expect(config).toBeNull()
const { config } = await loadConfig({ configPath }, logger)

await fs.rm(configFile)
expect(config).toBeNull()
} finally {
await fs.rm(tempDir, { recursive: true })
}
})

it('should read "lint-staged" key from package.yaml', async () => {
expect.assertions(1)
const tempDir = await createTempDir()
const configPath = path.join(tempDir, 'package.yaml')

const configFile = path.join(__dirname, '__mocks__', 'package.yaml')

await fs.writeFile(configFile, 'lint-staged:\n "*": mytask')

const { config } = await loadConfig({ configPath: configFile }, logger)

expect(config).toMatchInlineSnapshot(`
{
"*": "mytask",
}
`)
try {
expect.assertions(1)

await fs.writeFile(
configPath,
`lint-staged:
"*": mytask
`
)

const { config } = await loadConfig({ configPath }, logger)

expect(config).toMatchInlineSnapshot(`
{
"*": "mytask",
}
`)
} finally {
await fs.rm(tempDir, { recursive: true })
}
})

it('should read "lint-staged" key from package.yml', async () => {
expect.assertions(1)

const configFile = path.join(__dirname, '__mocks__', 'package.yml')

await fs.writeFile(configFile, 'lint-staged:\n "*": mytask')
const tempDir = await createTempDir()
const configPath = path.join(tempDir, 'package.yml')

const { config } = await loadConfig({ configPath: configFile }, logger)

expect(config).toMatchInlineSnapshot(`
{
"*": "mytask",
}
`)
try {
expect.assertions(1)

await fs.writeFile(
configPath,
`lint-staged:
"*": mytask
`
)

const { config } = await loadConfig({ configPath }, logger)

expect(config).toMatchInlineSnapshot(`
{
"*": "mytask",
}
`)
} finally {
await fs.rm(tempDir, { recursive: true })
}
})

it('should return null config when package.yaml file is invalid', async () => {
expect.assertions(1)
const tempDir = await createTempDir()
const configPath = path.join(tempDir, 'package.yaml')

const configFile = path.join(__dirname, '__mocks__', 'package.yaml')

await fs.writeFile(configFile, '{')
try {
expect.assertions(1)

const { config } = await loadConfig({ configPath: configFile }, logger)
await fs.writeFile(configPath, '{')

expect(config).toBeNull()
const { config } = await loadConfig({ configPath }, logger)

await fs.rm(configFile)
expect(config).toBeNull()
} finally {
await fs.rm(tempDir, { recursive: true })
}
})
})

0 comments on commit 4844150

Please sign in to comment.