Skip to content

Commit

Permalink
Refactor to add test utils (#6358)
Browse files Browse the repository at this point in the history
New helpers aim to reduce duplication in the test code.
  • Loading branch information
ybiquitous committed Sep 23, 2022
1 parent d57845b commit 005f39e
Show file tree
Hide file tree
Showing 12 changed files with 85 additions and 135 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@ node_modules
yarn.lock
.vscode/settings.json
.idea
tmp/
15 changes: 4 additions & 11 deletions lib/__tests__/extends.test.js
@@ -1,9 +1,11 @@
'use strict';

const path = require('path');

const safeChdir = require('../testUtils/safeChdir');
const configExtendingAnotherExtend = require('./fixtures/config-extending-another-extend.json');
const configExtendingOne = require('./fixtures/config-extending-one');
const configExtendingThreeWithOverride = require('./fixtures/config-extending-three-with-override');
const path = require('path');
const standalone = require('../standalone');

const fixturesPath = path.join(__dirname, 'fixtures');
Expand Down Expand Up @@ -66,16 +68,7 @@ it('extending a config that is overridden', async () => {
});

describe('extending a config from process.cwd', () => {
let actualCwd;

beforeAll(() => {
actualCwd = process.cwd();
process.chdir(__dirname);
});

afterAll(() => {
process.chdir(actualCwd);
});
safeChdir(__dirname);

it('works', async () => {
const linted = await standalone({
Expand Down
34 changes: 3 additions & 31 deletions lib/__tests__/ignore.test.js
Expand Up @@ -5,10 +5,13 @@ const path = require('path');
const NoFilesFoundError = require('../utils/noFilesFoundError');
const AllFilesIgnoredError = require('../utils/allFilesIgnoredError');
const replaceBackslashes = require('../testUtils/replaceBackslashes');
const safeChdir = require('../testUtils/safeChdir');
const standalone = require('../standalone');

const fixtures = (...elem) => replaceBackslashes(path.join(__dirname, 'fixtures', ...elem));

safeChdir(__dirname);

test('extending config and ignoreFiles glob ignoring single glob', async () => {
const { results } = await standalone({
files: [fixtures('empty-block.css'), fixtures('invalid-hex.css')],
Expand Down Expand Up @@ -39,10 +42,6 @@ test('extending config and ignoreFiles glob ignoring single glob', async () => {
});

test('same as above with no configBasedir, ignore-files path relative to process.cwd', async () => {
const actualCwd = process.cwd();

process.chdir(__dirname);

const { results } = await standalone({
files: [fixtures('empty-block.css'), fixtures('invalid-hex.css')],
config: {
Expand All @@ -68,8 +67,6 @@ test('same as above with no configBasedir, ignore-files path relative to process

// invalid-hex.css marked as ignored
expect(results[1].ignored).toBeTruthy();

process.chdir(actualCwd);
});

test('same as above with no configBasedir, ignore-files path relative to options.cwd', async () => {
Expand Down Expand Up @@ -163,9 +160,6 @@ test('extending config with ignoreFiles glob ignoring one by negation', async ()

test('specified `ignorePath` file ignoring one file', async () => {
const files = [fixtures('empty-block.css')];
const actualCwd = process.cwd();

process.chdir(__dirname);

await expect(
standalone({
Expand All @@ -178,15 +172,10 @@ test('specified `ignorePath` file ignoring one file', async () => {
ignorePath: [fixtures('ignore.txt')],
}),
).rejects.toThrow(AllFilesIgnoredError); // all files ignore

process.chdir(actualCwd);
});

test('specified multiple `ignorePath` to ignore files', async () => {
const files = [fixtures('empty-block.css'), fixtures('empty-block-with-disables.css')];
const actualCwd = process.cwd();

process.chdir(__dirname);

await expect(
standalone({
Expand All @@ -199,16 +188,12 @@ test('specified multiple `ignorePath` to ignore files', async () => {
ignorePath: [fixtures('ignore.txt'), fixtures('ignore-2.txt')],
}),
).rejects.toThrow(AllFilesIgnoredError); // all files ignore

process.chdir(actualCwd);
});

test('specified `ignorePath` file ignoring one file using options.cwd', async () => {
const files = [fixtures('empty-block.css')];
const allFilesIgnoredErrorMessage = new AllFilesIgnoredError(files);

process.chdir(__dirname);

await expect(
standalone({
files,
Expand All @@ -224,8 +209,6 @@ test('specified `ignorePath` file ignoring one file using options.cwd', async ()
});

test('specified `ignorePath` directory, not a file', async () => {
process.chdir(__dirname);

await expect(
standalone({
files: [],
Expand All @@ -238,9 +221,6 @@ test('specified `ignorePath` directory, not a file', async () => {

test('specified `ignorePattern` file ignoring one file', async () => {
const files = [fixtures('empty-block.css')];
const actualCwd = process.cwd();

process.chdir(__dirname);

await expect(
standalone({
Expand All @@ -253,8 +233,6 @@ test('specified `ignorePattern` file ignoring one file', async () => {
ignorePattern: 'fixtures/empty-block.css',
}),
).rejects.toThrow(AllFilesIgnoredError); // all files ignore

process.chdir(actualCwd);
});

test('specified `ignorePattern` file ignoring one file using options.cwd', async () => {
Expand All @@ -277,10 +255,6 @@ test('specified `ignorePattern` file ignoring one file using options.cwd', async
test('specified `ignorePattern` file ignoring two files', async () => {
const files = [fixtures('empty-block.css'), fixtures('no-syntax-error.css')];

const actualCwd = process.cwd();

process.chdir(__dirname);

await expect(
standalone({
files,
Expand All @@ -292,8 +266,6 @@ test('specified `ignorePattern` file ignoring two files', async () => {
ignorePattern: ['fixtures/empty-block.css', 'fixtures/no-syntax-error.css'],
}),
).rejects.toThrow(AllFilesIgnoredError); // all files ignore

process.chdir(actualCwd);
});

test('specified `ignorePattern` file ignoring two files using options.cwd', async () => {
Expand Down
13 changes: 3 additions & 10 deletions lib/__tests__/plugins.test.js
Expand Up @@ -3,6 +3,7 @@
const path = require('path');
const postcss = require('postcss');
const stylelint = require('..');
const safeChdir = require('../testUtils/safeChdir');

const cssWithFoo = '.foo {}';

Expand Down Expand Up @@ -251,7 +252,8 @@ it('plugin with async rule', async () => {
});

describe('loading a plugin from process.cwd', () => {
let actualCwd;
safeChdir(__dirname);

let result;

const config = {
Expand All @@ -261,15 +263,6 @@ describe('loading a plugin from process.cwd', () => {
},
};

beforeAll(() => {
actualCwd = process.cwd();
process.chdir(__dirname);
});

afterAll(() => {
process.chdir(actualCwd);
});

beforeEach(async () => {
result = await postcss().use(stylelint(config)).process('.foo {}', { from: undefined });
});
Expand Down
15 changes: 4 additions & 11 deletions lib/__tests__/postcssPlugin.test.js
@@ -1,8 +1,10 @@
'use strict';

const configurationError = require('../utils/configurationError');
const path = require('path');
const postcss = require('postcss');

const safeChdir = require('../testUtils/safeChdir');
const configurationError = require('../utils/configurationError');
const postcssPlugin = require('../postcssPlugin');

it('`config` option is `null`', () => {
Expand Down Expand Up @@ -95,16 +97,7 @@ it('`ignoreFiles` options is not empty and file not ignored', () => {
});

describe('stylelintignore', () => {
let actualCwd;

beforeEach(() => {
actualCwd = process.cwd();
process.chdir(__dirname);
});

afterEach(() => {
process.chdir(actualCwd);
});
safeChdir(__dirname);

it('postcssPlugin with .stylelintignore and file is ignored', () => {
const options = {
Expand Down
13 changes: 3 additions & 10 deletions lib/__tests__/processors.test.js
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const standalone = require('../standalone');

const fixturesPath = path.join(__dirname, './fixtures');
const safeChdir = require('../testUtils/safeChdir');

describe('processor transforms input and output', () => {
let results;
Expand Down Expand Up @@ -124,17 +125,9 @@ describe('multiple processors', () => {
});

describe('loading processors (and extend) from process.cwd', () => {
let actualCwd;
let results;

beforeAll(() => {
actualCwd = process.cwd();
process.chdir(path.join(__dirname, '..'));
});
safeChdir(path.join(__dirname, '..'));

afterAll(() => {
process.chdir(actualCwd);
});
let results;

beforeEach(async () => {
const code =
Expand Down
20 changes: 9 additions & 11 deletions lib/__tests__/standalone-cache.test.js
Expand Up @@ -6,17 +6,10 @@ const { promises: fs, existsSync } = require('fs');

const hash = require('../utils/hash');
const replaceBackslashes = require('../testUtils/replaceBackslashes');
const removeFile = require('../testUtils/removeFile');
const safeChdir = require('../testUtils/safeChdir');
const standalone = require('../standalone');

// NOTE: `fs.rm(file, { force: true })` will be available when we drop the support of older Node versions.
// See <https://nodejs.org/api/fs.html#fs_fs_rm_path_options_callback>
const removeFile = async (filePath) => {
if (existsSync(filePath)) {
await fs.unlink(filePath);
}
};

const cwd = process.cwd();
const fixturesPath = path.join(__dirname, 'fixtures');
const invalidFile = path.join(fixturesPath, 'empty-block.css');
const syntaxErrorFile = path.join(fixturesPath, 'syntax_error.css');
Expand All @@ -38,8 +31,9 @@ function getConfig(additional = {}) {
}

describe('standalone cache', () => {
// HACK: The test may fail depending on the timing of the access to the cache file. See #5531
jest.retryTimes(3);
const cwd = path.join(__dirname, 'tmp', 'standalone-cache');

safeChdir(cwd);

const expectedCacheFilePath = path.join(cwd, '.stylelintcache');

Expand Down Expand Up @@ -162,6 +156,10 @@ describe('standalone cache', () => {
});

describe('standalone cache uses cacheLocation', () => {
const cwd = path.join(__dirname, 'tmp', 'standalone-cache-uses-cacheLocation');

safeChdir(cwd);

const cacheLocationFile = path.join(fixturesPath, 'cache', '.cachefile');
const cacheLocationDir = path.join(fixturesPath, 'cache');
const expectedCacheFilePath = path.join(cacheLocationDir, `.stylelintcache_${hash(cwd)}`);
Expand Down
19 changes: 9 additions & 10 deletions lib/__tests__/standalone-fix.test.js
@@ -1,13 +1,14 @@
'use strict';

const os = require('os');
const path = require('path');
const stripIndent = require('common-tags').stripIndent;
const { existsSync, promises: fs } = require('fs');
const { stripIndent } = require('common-tags');
const fs = require('fs').promises;

const removeFile = require('../testUtils/removeFile');
const replaceBackslashes = require('../testUtils/replaceBackslashes');
const standalone = require('../standalone');
const safeChdir = require('../testUtils/safeChdir');
const uniqueId = require('../testUtils/uniqueId');
const standalone = require('../standalone');

const fixturesPath = (...elems) => replaceBackslashes(path.join(__dirname, 'fixtures', ...elems));

Expand Down Expand Up @@ -150,20 +151,18 @@ it("the indentation rule doesn't fix with scoped stylelint-disable commands", as
});

describe('writing fixes to files', () => {
let tmpDir;
safeChdir(path.join(__dirname, 'tmp', `standalone-fix-${uniqueId()}`));

let tempFile;

beforeEach(async () => {
tmpDir = os.tmpdir();
tempFile = replaceBackslashes(path.join(tmpDir, `stylesheet-${uniqueId()}.css`));
tempFile = replaceBackslashes(path.join(process.cwd(), 'stylesheet.css'));

await fs.copyFile(fixturesPath('fix.css'), tempFile);
});

afterEach(async () => {
if (existsSync(tempFile)) {
await fs.unlink(tempFile);
}
await removeFile(tempFile);
});

it('overwrites the original file', async () => {
Expand Down

0 comments on commit 005f39e

Please sign in to comment.