Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor system tests #4755

Merged
merged 3 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`
124 changes: 123 additions & 1 deletion lib/__tests__/cli.test.js
@@ -1,6 +1,17 @@
/* eslint no-console: off */

'use strict';

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

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

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 +154,114 @@ 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('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.
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