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

Modernize unit tests #318

Merged
merged 3 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions __tests__/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# How to add tests

* Create your test file named after the scss lint your you are adding a stylelint rule for. For example, `__tests___/unit/bang-format.spec.js`
* Create your test file named after the scss lint your you are adding a stylelint rule for. For example, `__tests___/unit/bang-format.test.mjs`
* Copy the code from one of the other tests into your test new file
* Update the test so you have .scss code that will produce lint you want to test for
* Add a console.log statement within the checkResult function in the test file passing `result` to the log statement. e.g. `console.log(result)`
* Look for warnings it generates
* Copy expected text to test file
* Escape quotes in your expected error text
* Run tests again and refine until they pass
* Update `t.plan(2)` to the number of tests you have
49 changes: 49 additions & 0 deletions __tests__/border-none.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { beforeEach, describe, it } from 'node:test';
import assert from 'node:assert/strict';

import stylelint from 'stylelint';

import config from '../index.js';

describe('flags warnings with border none', () => {
const invalidScss = (
`.borderzero {
border: none;
}
`);

let result;

beforeEach(async () => {
result = await stylelint.lint({
code: invalidScss,
config,
});
});

it('did error', () => {
assert.equal(result.errored, true);
});

it('flags warnings', () => {
assert.equal(result.results[0].warnings.length, 1);
});

it('correct warning text', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.text),
[
'Unexpected value "none" for property "border" (declaration-property-value-disallowed-list)',
],
);
});

it('correct rule flagged', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.rule),
[
'declaration-property-value-disallowed-list',
],
);
});
});
49 changes: 49 additions & 0 deletions __tests__/color-keyword.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { beforeEach, describe, it } from 'node:test';
import assert from 'node:assert/strict';

import stylelint from 'stylelint';

import config from '../index.js';

describe('flags warnings with named color keyword', () => {
const invalidScss = (
`.colorkeyword {
color: green;
}
`);

let result;

beforeEach(async () => {
result = await stylelint.lint({
code: invalidScss,
config,
});
});

it('did error', () => {
assert.equal(result.errored, true);
});

it('flags warnings', () => {
assert.equal(result.results[0].warnings.length, 1);
});

it('correct warning text', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.text),
[
'Unexpected named color "green" (color-named)',
],
);
});

it('correct rule flagged', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.rule),
[
'color-named',
],
);
});
});
51 changes: 51 additions & 0 deletions __tests__/debug-statement.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { beforeEach, describe, it } from 'node:test';
import assert from 'node:assert/strict';

import stylelint from 'stylelint';

import config from '../index.js';

describe('flags warnings with debug statement scss', () => {
const invalidScss = (
`$color-blue: #1c94c6;

.debug {
@debug $color-blue;
}
`);

let result;

beforeEach(async () => {
result = await stylelint.lint({
code: invalidScss,
config,
});
});

it('did error', () => {
assert.equal(result.errored, true);
});

it('flags warnings', () => {
assert.equal(result.results[0].warnings.length, 1);
});

it('correct warning text', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.text),
[
'Unexpected at-rule "debug" (at-rule-disallowed-list)',
],
);
});

it('correct rule flagged', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.rule),
[
'at-rule-disallowed-list',
],
);
});
});
57 changes: 57 additions & 0 deletions __tests__/empty-line-between-blocks.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { beforeEach, describe, it } from 'node:test';
import assert from 'node:assert/strict';

import stylelint from 'stylelint';

import config from '../index.js';

describe('flags warnings with missing empty line between blocks', () => {
const invalidScss = (
`p {
margin: 0;
em {
color: #f00;
}
}
a {
color: #f00;
}
`);

let result;

beforeEach(async () => {
result = await stylelint.lint({
code: invalidScss,
config,
});
});

it('did error', () => {
assert.equal(result.errored, true);
});

it('flags warnings', () => {
assert.equal(result.results[0].warnings.length, 2);
});

it('correct warning text', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.text),
[
'Expected empty line before rule (rule-empty-line-before)',
'Expected empty line before rule (rule-empty-line-before)',
],
);
});

it('correct rule flagged', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.rule),
[
'rule-empty-line-before',
'rule-empty-line-before',
],
);
});
});
48 changes: 48 additions & 0 deletions __tests__/empty-rule.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { beforeEach, describe, it } from 'node:test';
import assert from 'node:assert/strict';

import stylelint from 'stylelint';

import config from '../index.js';

describe('flags warnings with empty rule', () => {
const invalidScss = (
`.cat {
}
`);

let result;

beforeEach(async () => {
result = await stylelint.lint({
code: invalidScss,
config,
});
});

it('did error', () => {
assert.equal(result.errored, true);
});

it('flags warnings', () => {
assert.equal(result.results[0].warnings.length, 1);
});

it('correct warning text', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.text),
[
'Unexpected empty block (block-no-empty)',
],
);
});

it('correct rule flagged', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.rule),
[
'block-no-empty',
],
);
});
});
49 changes: 49 additions & 0 deletions __tests__/hex-length.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { beforeEach, describe, it } from 'node:test';
import assert from 'node:assert/strict';

import stylelint from 'stylelint';

import config from '../index.js';

describe('flags warnings with hex color length', () => {
const invalidScss = (
`.hexlength {
color: #ff22ee;
}
`);

let result;

beforeEach(async () => {
result = await stylelint.lint({
code: invalidScss,
config,
});
});

it('did error', () => {
assert.equal(result.errored, true);
});

it('flags warnings', () => {
assert.equal(result.results[0].warnings.length, 1);
});

it('correct warning text', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.text),
[
'Expected "#ff22ee" to be "#f2e" (color-hex-length)',
],
);
});

it('correct rule flagged', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.rule),
[
'color-hex-length',
],
);
});
});
49 changes: 49 additions & 0 deletions __tests__/hex-validation.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { beforeEach, describe, it } from 'node:test';
import assert from 'node:assert/strict';

import stylelint from 'stylelint';

import config from '../index.js';

describe('flags warnings with hex validation', () => {
const invalidScss = (
`.hex-validation {
background: #ab; // Clearly a typo
}
`);

let result;

beforeEach(async () => {
result = await stylelint.lint({
code: invalidScss,
config,
});
});

it('did error', () => {
assert.equal(result.errored, true);
});

it('flags warnings', () => {
assert.equal(result.results[0].warnings.length, 1);
});

it('correct warning text', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.text),
[
'Unexpected invalid hex color "#ab" (color-no-invalid-hex)',
],
);
});

it('correct rule flagged', () => {
assert.deepEqual(
result.results[0].warnings.map((w) => w.rule),
[
'color-no-invalid-hex',
],
);
});
});
Loading
Loading