Skip to content

Commit

Permalink
Refactor system tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeddy3 committed May 8, 2020
1 parent 0376cf9 commit 3599722
Show file tree
Hide file tree
Showing 74 changed files with 1,910 additions and 34,435 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Expand Up @@ -12,3 +12,4 @@
lib/utils/parseCalcExpression/parser.js
scripts/**/*.css
system-tests/**/*.css
system-tests/**/*.scss
2 changes: 1 addition & 1 deletion docs/developer-guide/syntaxes.md
Expand Up @@ -31,5 +31,5 @@ stylelint currently turns off autofix for sources that contain:
To add a new workaround, you should:

1. Add code to [`lib/lintSource.js`](https://github.com/stylelint/stylelint/blob/master/lib/lintSource.js) to detect the incompatible pattern.
2. Add a corresponding test to [`system-tests/fix/fix.test.js`](https://github.com/stylelint/stylelint/blob/master/system-tests/fix/fix.test.js).
2. Add a corresponding test to [`lib/__tests__/standalone-fix.test.js`](https://github.com/stylelint/stylelint/blob/master/lib/__tests__/standalone-fix.test.js).
3. Document the workaround in [`docs/developer-guides/syntaxes.md`](https://github.com/stylelint/stylelint/blob/master/docs/developer-guide/syntaxes.md).
4 changes: 2 additions & 2 deletions docs/developer-guide/system-tests.md
Expand Up @@ -19,5 +19,5 @@ To add a system test, you should:

- add a test-case folder to `system-tests/` incrementing the number from existing test cases
- add a configuration file and a stylesheet
- setup the test following the format established by existing tests, and using the `systemTestUtils`
- take a snapshot of the JSON `results` array
- add a `fs.test.js` and `no-fs.test.js` following the format established by existing tests, and using the `systemTestUtils`
- take a snapshot of `output`
File renamed without changes.
150 changes: 149 additions & 1 deletion lib/__tests__/cli.test.js
@@ -1,6 +1,23 @@
/* eslint no-console: off */

'use strict';

const { buildCLI } = require('../cli');
const _ = require('lodash');

const fs = require('fs');
const os = require('os');
const path = require('path');
const { promisify } = require('util');

const cli = require('../cli');
const pkg = require('../../package.json');
const replaceBackslashes = require('../testUtils/replaceBackslashes');

const readFile = promisify(fs.readFile);
const fixturesPath = replaceBackslashes(path.join(__dirname, 'fixtures'));
const { buildCLI } = cli;

jest.mock('get-stdin');

describe('buildCLI', () => {
it('flags - default', () => {
Expand Down Expand Up @@ -143,3 +160,134 @@ describe('buildCLI', () => {
expect(buildCLI(['-v']).flags.version).toBe(true);
});
});

describe('CLI', () => {
beforeAll(() => {
jest.spyOn(process, 'exit').mockImplementation(() => {});
jest.spyOn(process.stdout, 'write').mockImplementation(() => {});
jest.spyOn(console, 'log').mockImplementation(() => {});
});

afterAll(() => {
jest.restoreAllMocks();
});

it('basic', async () => {
await cli([]);

expect(process.exit).toHaveBeenCalledWith(2);

expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith(
expect.stringContaining('Usage: stylelint [input] [options]'),
);
});

it('--help', async () => {
await cli(['--help']);

expect(process.exit).toHaveBeenCalledWith(0);

expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log.mock.calls[0][0]).toMatchSnapshot();
});

it('--version', async () => {
await cli(['--version']);

expect(process.exitCode).toBeUndefined();

expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith(expect.stringContaining(pkg.version));
});

it('--print-config', async () => {
await cli([
'--print-config',
'--config',
`${fixturesPath}/config-color-no-invalid-hex.json`,
`${fixturesPath}/invalid-hex.css`,
]);

expect(process.exitCode).toBeUndefined();

expect(process.stdout.write).toHaveBeenCalledTimes(1);
expect(process.stdout.write).toHaveBeenLastCalledWith(
JSON.stringify(
{
rules: {
'color-no-invalid-hex': [true],
},
},
null,
' ',
),
);
});

it('--report-needless-disables', async () => {
await cli([
'--report-needless-disables',
'--config',
replaceBackslashes(path.join(fixturesPath, 'config-block-no-empty.json')),
replaceBackslashes(path.join(fixturesPath, 'empty-block-with-disables.css')),
]);

expect(process.exitCode).toBe(2);

expect(process.stdout.write).toHaveBeenCalledTimes(2);
expect(process.stdout.write).toHaveBeenNthCalledWith(
1,
expect.stringContaining('unused rule: color-named'),
);
expect(process.stdout.write).toHaveBeenNthCalledWith(
2,
expect.stringContaining('Unexpected empty block'),
);
});

it('--stdin', async () => {
await cli(['--stdin', '--config', `${fixturesPath}/config-no-empty-source.json`]);

expect(process.exitCode).toBe(2);

expect(process.stdout.write).toHaveBeenCalledTimes(1);
expect(process.stdout.write).toHaveBeenNthCalledWith(
1,
expect.stringContaining('Unexpected empty source'),
);
});

it('--output-file', async () => {
const outPath = path.join(os.tmpdir(), `out-${_.uniqueId()}.txt`);

await cli([
'--output-file',
outPath,
'--formatter',
'json',
'--config',
replaceBackslashes(path.join(fixturesPath, 'config-color-no-invalid-hex.json')),
replaceBackslashes(path.join(fixturesPath, 'invalid-hex.css')),
]);

const content = await readFile(outPath, 'utf-8');

expect(console.log).toHaveBeenCalledTimes(0);
expect(process.stdout.write).toHaveBeenCalledTimes(1);
expect(process.stdout.write).toHaveBeenNthCalledWith(1, expect.stringContaining(content));
});

it('exits with non zero on unfound module in config', async () => {
await cli([
'--config',
replaceBackslashes(path.join(fixturesPath, 'config-require-unknown.js')),
replaceBackslashes(path.join(fixturesPath, 'empty-block-with-disables.css')),
]);

expect(process.exit).not.toHaveBeenCalledWith(0);

expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('Cannot find module'));
});
});
@@ -1,6 +1,5 @@
{
"rules": {
"block-no-empty": true,
"no-empty-source": true
}
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/__tests__/ignore.test.js
Expand Up @@ -2,7 +2,7 @@

const NoFilesFoundError = require('../utils/noFilesFoundError');
const path = require('path');
const replaceBackslashes = require('./replaceBackslashes');
const replaceBackslashes = require('../testUtils/replaceBackslashes');
const standalone = require('../standalone');

const fixturesPath = replaceBackslashes(path.join(__dirname, 'fixtures'));
Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/invalidScopeDisables.test.js
Expand Up @@ -2,7 +2,7 @@

const invalidScopeDisables = require('../invalidScopeDisables');
const path = require('path');
const replaceBackslashes = require('./replaceBackslashes');
const replaceBackslashes = require('../testUtils/replaceBackslashes');
const standalone = require('../standalone');
const stripIndent = require('common-tags').stripIndent;

Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/needlessDisables.test.js
Expand Up @@ -2,7 +2,7 @@

const needlessDisables = require('../needlessDisables');
const path = require('path');
const replaceBackslashes = require('./replaceBackslashes');
const replaceBackslashes = require('../testUtils/replaceBackslashes');
const standalone = require('../standalone');
const stripIndent = require('common-tags').stripIndent;

Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/printConfig.test.js
Expand Up @@ -3,7 +3,7 @@
const path = require('path');
const pluginWarnAboutFoo = require('./fixtures/plugin-warn-about-foo');
const printConfig = require('../printConfig');
const replaceBackslashes = require('./replaceBackslashes');
const replaceBackslashes = require('../testUtils/replaceBackslashes');

it('printConfig uses getConfigForFile to retrieve the config', () => {
const filepath = replaceBackslashes(
Expand Down
2 changes: 1 addition & 1 deletion lib/__tests__/standalone-cache.test.js
Expand Up @@ -5,7 +5,7 @@ const path = require('path');
const standalone = require('../standalone');
const fixturesPath = path.join(__dirname, 'fixtures');
const fs = require('fs');
const replaceBackslashes = require('./replaceBackslashes');
const replaceBackslashes = require('../testUtils/replaceBackslashes');
const { promisify } = require('util');
const unlink = promisify(fs.unlink);
const fCache = require('file-entry-cache');
Expand Down

0 comments on commit 3599722

Please sign in to comment.