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

Fix tightly-coupled dependency on Stylelint's internal module lib/utils/getOsEol #66

Merged
merged 4 commits into from
Jul 26, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Head

- Fixed: tightly-coupled dependency on Stylelint's internal module `lib/utils/getOsEol`.

## 6.1.0

- Added: support for custom unfixable error messages.
Expand Down
1 change: 1 addition & 0 deletions __tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports[`should contain expected keys 1`] = `
[
"setupFiles",
"setupFilesAfterEnv",
"testEnvironment",
]
`;
10 changes: 8 additions & 2 deletions getTestRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

const util = require('util');

const { lint } = require('stylelint'); // eslint-disable-line n/no-unpublished-require -- Avoid auto-install of `stylelint` peer dependency.

/**
* @typedef {import('.').TestCase} TestCase
* @typedef {import('.').TestSchema} TestSchema
Expand All @@ -12,6 +10,14 @@ const { lint } = require('stylelint'); // eslint-disable-line n/no-unpublished-r
/** @type {import('.').getTestRule} */
module.exports = function getTestRule(options = {}) {
return function testRule(schema) {
/** @type {import('stylelint').lint} */
let lint;

beforeAll(() => {
// eslint-disable-next-line n/no-unpublished-require -- Avoid auto-install of `stylelint` peer dependency.
lint = require('stylelint').lint;
});

describe(`${schema.ruleName}`, () => {
const stylelintConfig = {
plugins: options.plugins || schema.plugins,
Expand Down
1 change: 1 addition & 0 deletions jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

module.exports = {
setupFiles: [require.resolve('./jest-setup.js')],
setupFilesAfterEnv: [require.resolve('./jest-setup-after-env.js')],
testEnvironment: 'node',
};
18 changes: 18 additions & 0 deletions jest-setup-after-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const os = require('os');

const eolDescriptor = Object.getOwnPropertyDescriptor(os, 'EOL');

if (!eolDescriptor) {
throw new TypeError('`os` must have an `EOL` property');
}

beforeAll(() => {
// NOTE: `jest.replaceProperty()` is unavailable for a read-only property.
Object.defineProperty(os, 'EOL', { ...eolDescriptor, value: '\n' });
});

afterAll(() => {
Object.defineProperty(os, 'EOL', eolDescriptor);
});
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 0 additions & 3 deletions jest-setup.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';

// Mock should be before stylelint required. Even if it's required inside other modules
jest.mock('stylelint/lib/utils/getOsEol', () => () => '\n');

const getTestRule = require('./getTestRule');

global.testRule = getTestRule();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"getTestRule.js",
"jest-preset.js",
"jest-setup.js",
"jest-setup-after-env.js",
"index.d.ts"
],
"scripts": {
Expand Down
Loading