Skip to content

Commit

Permalink
Merge pull request #29 from adorade/develop
Browse files Browse the repository at this point in the history
Update tests
  • Loading branch information
adorade authored Aug 13, 2024
2 parents d77434d + 461416d commit ca4cabf
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/reporter-factory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function reporterFactory(config = {}, options = {}) {
*/
if (config.console && formattedText.trim()) {
asyncTasks.push(
fancyLog.info(`\n${formattedText}\n`)
fancyLog.info(`${formattedText}`)
);
}

Expand Down
196 changes: 196 additions & 0 deletions test/fail.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*!
* Gulp Stylelint (v2.1.0): test/fail-after-error.test.js
* Copyright (c) 2023-24 Adorade (https://github.com/adorade/gulp-stylelint-esm)
* License under MIT
* ========================================================================== */

import gulp from 'gulp';

import path from 'node:path';
import url from 'node:url';

import gStylelintEsm from '../src/index.mjs';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

function fixtures(glob) {
return path.join(__dirname, 'fixtures', glob);
}

describe('Fail after error', () => {
test('should not fail after emitting no errors if `failAfterError` is not configured', async () => {
const stream = gulp.src(fixtures('basic.css'));

await new Promise((resolve) => {
stream
.pipe(gStylelintEsm({
config: { rules: {} }
}))
.on('error', (error) => {
expect(error).toBe(undefined);
resolve;
})
.on('finish', resolve);
});
});
test('should not fail after emitting no errors if `failAfterError` is set to true', async () => {
const stream = gulp.src(fixtures('basic.css'));

await new Promise((resolve) => {
stream
.pipe(gStylelintEsm({
failAfterError: true,
config: { rules: {} }
}))
.on('error', (error) => {
expect(error).toBe(undefined);
resolve;
})
.on('finish', resolve);
});
});
test('should not fail after emitting no errors if `failAfterError` is set to false', async () => {
const stream = gulp.src(fixtures('basic.css'));

await new Promise((resolve) => {
stream
.pipe(gStylelintEsm({
failAfterError: false,
config: { rules: {} }
}))
.on('error', (error) => {
expect(error).toBe(undefined);
resolve;
})
.on('finish', resolve);
});
});
test('should fail after emitting an error if `failAfterError` is not configured', async () => {
const stream = gulp.src(fixtures('invalid.css'));
let errorEmitted = false;

expect.assertions(2);

await new Promise((resolve) => {
stream
.pipe(gStylelintEsm({
config: { rules: { 'color-hex-length': 'short' } },
}))
.on('error', (error) => {
errorEmitted = true;
expect(error.message).toBe('Failed with 1 error');
resolve;
})
.on('finish', resolve);
});

expect(errorEmitted).toBe(true);
});
test('should fail after emitting errors if `failAfterError` is not configured', async () => {
const stream = gulp.src(fixtures('invalids.css'));
let errorEmitted = false;

expect.assertions(2);

await new Promise((resolve) => {
stream
.pipe(gStylelintEsm({
config: { rules: { 'color-hex-length': 'short' } },
}))
.on('error', (error) => {
errorEmitted = true;
expect(error.message).toBe('Failed with 2 errors');
resolve;
})
.on('finish', resolve);
});

expect(errorEmitted).toBe(true);
});
test('should fail after emitting an error if `failAfterError` is set to true', async () => {
const stream = gulp.src(fixtures('invalid.css'));
let errorEmitted = false;

expect.assertions(2);

await new Promise((resolve) => {
stream
.pipe(gStylelintEsm({
failAfterError: true,
config: { rules: { 'color-hex-length': 'short' } },
}))
.on('error', (error) => {
errorEmitted = true;
expect(error.message).toBe('Failed with 1 error');
resolve;
})
.on('finish', resolve);
});

expect(errorEmitted).toBe(true);
});
test('should fail after emitting errors if `failAfterError` is set to true', async () => {
const stream = gulp.src(fixtures('invalids.css'));
let errorEmitted = false;

expect.assertions(2);

await new Promise((resolve) => {
stream
.pipe(gStylelintEsm({
failAfterError: true,
config: { rules: { 'color-hex-length': 'short' } },
}))
.on('error', (error) => {
errorEmitted = true;
expect(error.message).toBe('Failed with 2 errors');
resolve;
})
.on('finish', resolve);
});

expect(errorEmitted).toBe(true);
});
test('should not fail after emitting an error if `failAfterError` is set to false', async () => {
const stream = gulp.src(fixtures('invalid.css'));
let errorEmitted = false;

expect.assertions(1);

await new Promise((resolve) => {
stream
.pipe(gStylelintEsm({
failAfterError: false,
config: { rules: { 'color-hex-length': 'short' } },
}))
.on('error', () => {
errorEmitted = true;
resolve;
})
.on('finish', resolve);
});

expect(errorEmitted).toBe(false);
});
test('should not fail after emitting errors if `failAfterError` is set to false', async () => {
const stream = gulp.src(fixtures('invalids.css'));
let errorEmitted = false;

expect.assertions(1);

await new Promise((resolve) => {
stream
.pipe(gStylelintEsm({
failAfterError: false,
config: { rules: { 'color-hex-length': 'short' } },
}))
.on('error', () => {
errorEmitted = true;
resolve;
})
.on('finish', resolve);
});

expect(errorEmitted).toBe(false);
});
});
4 changes: 4 additions & 0 deletions test/fixtures/invalids.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.foo {
background-color: #111111;
color: #ffffff;
}
2 changes: 2 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ describe('Plugin Functionality', () => {
test('should emit an error when linter complains', async () => {
const stream = gulp.src(fixtures('invalid.css'));

expect.assertions(1);

try {
await new Promise((resolve, reject) => {
stream
Expand Down
2 changes: 1 addition & 1 deletion test/reporter-factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Reporter Functionality', () => {

reporter({});

expect(fancyLog.info.calledWith('\nfoo\n')).toBeTruthy();
expect(fancyLog.info.calledWith('foo')).toBeTruthy();

fancyLog.info.restore();
});
Expand Down

0 comments on commit ca4cabf

Please sign in to comment.