Skip to content

Commit

Permalink
Convert tests to Node test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
robatwilliams committed Jan 30, 2024
1 parent ca10780 commit 76d76a1
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
const assert = require('node:assert');
const { test } = require('node:test');
const compareVersions = require('./compareVersions');

test('equal', () => {
expect(compareVersions('1.2.3', '1.2.3')).toBe(0);
assert.strictEqual(compareVersions('1.2.3', '1.2.3'), 0);
});

test('smaller', () => {
expect(compareVersions('1.2.0', '1.2.3')).toBe(-1);
expect(compareVersions('1.2', '1.3')).toBe(-1);
assert.strictEqual(compareVersions('1.2.0', '1.2.3'), -1);
assert.strictEqual(compareVersions('1.2', '1.3'), -1);
});

test('larger', () => {
expect(compareVersions('1.2.3', '1.2.0')).toBe(1);
assert.strictEqual(compareVersions('1.2.3', '1.2.0'), 1);
});

test('partial on one side', () => {
expect(compareVersions('1.1', '1.2.3')).toBe(-1);
expect(compareVersions('1.1.1', '1.2')).toBe(-1);
assert.strictEqual(compareVersions('1.1', '1.2.3'), -1);
assert.strictEqual(compareVersions('1.1.1', '1.2'), -1);
});

test('when it matters to treat parts as numbers', () => {
expect(compareVersions('9', '10')).toBe(-1);
assert.strictEqual(compareVersions('9', '10'), -1);
});
33 changes: 18 additions & 15 deletions packages/eslint-plugin-ecmascript-compat/lib/compatibility.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable camelcase */
const assert = require('node:assert');
const { it } = require('node:test');
const { unsupportedFeatures } = require('./compatibility');
const features = require('./features');

Expand All @@ -16,7 +18,7 @@ it('supports feature in version introduced', () => {
};

const unsupported = unsupportedFeatures([feature], [{ name: 'chrome', version: '73' }]);
expect(unsupported).toHaveLength(0);
assert.deepStrictEqual(unsupported, []);
});

it('supports feature in version later than introduced, treating versions as numbers', () => {
Expand All @@ -36,7 +38,7 @@ it('supports feature in version later than introduced, treating versions as numb
[feature],
[{ name: 'safari', version: '14.0' }]
);
expect(unsupported).toHaveLength(0);
assert.deepStrictEqual(unsupported, []);
});

it('doesnt support feature in version before introduced', () => {
Expand All @@ -53,7 +55,7 @@ it('doesnt support feature in version before introduced', () => {
};

const unsupported = unsupportedFeatures([feature], [{ name: 'chrome', version: '72' }]);
expect(unsupported[0]).toBe(feature);
assert.strictEqual(unsupported[0], feature);
});

it('supports feature supported by family in unknown version', () => {
Expand All @@ -70,7 +72,7 @@ it('supports feature supported by family in unknown version', () => {
};

const unsupported = unsupportedFeatures([feature], [{ name: 'chrome', version: '73' }]);
expect(unsupported).toHaveLength(0);
assert.deepStrictEqual(unsupported, []);
});

it('doesnt support feature not supported in any version of family', () => {
Expand All @@ -87,7 +89,7 @@ it('doesnt support feature not supported in any version of family', () => {
};

const unsupported = unsupportedFeatures([feature], [{ name: 'chrome', version: '73' }]);
expect(unsupported[0]).toBe(feature);
assert.strictEqual(unsupported[0], feature);
});

it('supports feature with unknown support by family', () => {
Expand All @@ -104,7 +106,7 @@ it('supports feature with unknown support by family', () => {
};

const unsupported = unsupportedFeatures([feature], [{ name: 'chrome', version: '73' }]);
expect(unsupported).toHaveLength(0);
assert.deepStrictEqual(unsupported, []);
});

it('supports feature with omitted support entry for mobile target', () => {
Expand All @@ -124,7 +126,7 @@ it('supports feature with omitted support entry for mobile target', () => {
[feature],
[{ name: 'chrome_android', version: '73' }]
);
expect(unsupported).toHaveLength(0);
assert.deepStrictEqual(unsupported, []);
});

it('doesnt support feature supported by one target but not another', () => {
Expand All @@ -148,7 +150,7 @@ it('doesnt support feature supported by one target but not another', () => {
{ name: 'firefox', version: '50' },
]
);
expect(unsupported[0]).toBe(feature);
assert.strictEqual(unsupported[0], feature);
});

it('uses primary support record where multiple ones exist', () => {
Expand Down Expand Up @@ -179,13 +181,13 @@ it('uses primary support record where multiple ones exist', () => {
[feature],
[{ name: 'nodejs', version: '7.0.0' }]
);
expect(primaryUnsupported).toHaveLength(0);
assert.deepStrictEqual(primaryUnsupported, []);

const secondaryUnsupported = unsupportedFeatures(
[feature],
[{ name: 'nodejs', version: '6.7.0' }]
);
expect(secondaryUnsupported[0]).toBe(feature);
assert.deepStrictEqual(secondaryUnsupported[0], feature);
});

it('explains what the problem is when compat feature not found in MDN data', () => {
Expand All @@ -211,9 +213,10 @@ it('explains what the problem is when compat feature not found in MDN data', ()
],
};

expect(() => {
unsupportedFeatures([feature], [{ name: 'chrome', version: '73' }]);
}).toThrow("Sparse compatFeatures for rule 'some rule': object,undefined");
assert.throws(
() => unsupportedFeatures([feature], [{ name: 'chrome', version: '73' }]),
{ message: "Sparse compatFeatures for rule 'some rule': object,undefined" }
);
});

it('can rely on all the versions in the compatibility data used being semver or partial semver', () => {
Expand All @@ -229,8 +232,8 @@ it('can rely on all the versions in the compatibility data used being semver or
: supportStatement;

if (simpleSupportStatement.version_added !== false) {
// eslint-disable-next-line jest/no-conditional-expect
expect(simpleSupportStatement.version_added).toMatch(
assert.match(
simpleSupportStatement.version_added,
/\d+(?<minor>\.\d+(?<patch>\.\d+)?)?/u
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { RuleTester } = require('eslint');

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 46';
jest.resetModules();

const ruleTester = new RuleTester({
parserOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { RuleTester } = require('eslint');

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 53';
jest.resetModules();

const ruleTester = new RuleTester({
parserOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { RuleTester } = require('eslint');

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 59';
jest.resetModules();

const ruleTester = new RuleTester({
parserOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { RuleTester } = require('eslint');

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 65';
jest.resetModules();

const ruleTester = new RuleTester({
parserOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { RuleTester } = require('eslint');

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 62';
jest.resetModules();

const ruleTester = new RuleTester({
parserOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { RuleTester } = require('eslint');

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 74';
jest.resetModules();

const ruleTester = new RuleTester({
parserOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { RuleTester } = require('eslint');

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 71';
jest.resetModules();

const ruleTester = new RuleTester({
parserOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { RuleTester } = require('eslint');

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 73';
jest.resetModules();

const ruleTester = new RuleTester({
parserOptions: {
Expand Down
4 changes: 3 additions & 1 deletion packages/eslint-plugin-ecmascript-compat/lib/rule.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const assert = require('node:assert');
const { test } = require('node:test');
const features = require('./features');
const rule = require('./rule');

Expand All @@ -7,5 +9,5 @@ test('polyfills accepted in the schema exactly match those supported', () => {
feature.polyfill ? [feature.polyfill] : []
);

expect(schemaPolyfills.sort()).toEqual(supportedPolyfills.sort());
assert.deepStrictEqual(schemaPolyfills.sort(), supportedPolyfills.sort());
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const assert = require('node:assert');
const { beforeEach, it } = require('node:test');
const browserslist = require('browserslist');
const targetRuntimes = require('./targetRuntimes');

Expand All @@ -9,38 +11,38 @@ beforeEach(() => {
it('targets the oldest version of each family', () => {
process.env.BROWSERSLIST = 'Chrome >= 50';

expect(targetRuntimes()).toEqual([{ name: 'chrome', version: '50' }]);
assert.deepStrictEqual(targetRuntimes(), [{ name: 'chrome', version: '50' }]);
});

it('targets the oldest version of each family, treating versions as numbers', () => {
process.env.BROWSERSLIST = 'chrome 9-10';

expect(targetRuntimes()).toEqual([{ name: 'chrome', version: '9' }]);
assert.deepStrictEqual(targetRuntimes(), [{ name: 'chrome', version: '9' }]);
});

it('maps browserslist names to mdn names where necessary', () => {
process.env.BROWSERSLIST = 'Android >= 0';

expect(targetRuntimes()).toEqual([{ name: 'webview_android', version: '2.1' }]);
assert.deepStrictEqual(targetRuntimes(), [{ name: 'webview_android', version: '2.1' }]);
});

it('discards families unknown to mdn', () => {
process.env.BROWSERSLIST = 'kaios >= 0';

expect(targetRuntimes()).toEqual([]);
assert.deepStrictEqual(targetRuntimes(), []);
});

it('handles multiple families', () => {
process.env.BROWSERSLIST = 'Firefox >= 55,IE >= 10';

expect(targetRuntimes()).toEqual([
assert.deepStrictEqual(targetRuntimes(), [
{ name: 'firefox', version: '55' },
{ name: 'ie', version: '10' },
]);
});

it('accepts a override', () => {
expect(targetRuntimes('Firefox >= 55,IE >= 10')).toEqual([
assert.deepStrictEqual(targetRuntimes('Firefox >= 55,IE >= 10'), [
{ name: 'firefox', version: '55' },
{ name: 'ie', version: '10' },
]);
Expand All @@ -49,13 +51,13 @@ it('accepts a override', () => {
it('preserves versions as strings to allow semver', () => {
process.env.BROWSERSLIST = 'Node >= 0';

expect(targetRuntimes()).toEqual([{ name: 'nodejs', version: '0.2.0' }]);
assert.deepStrictEqual(targetRuntimes(), [{ name: 'nodejs', version: '0.2.0' }]);
});

it('simplifies version ranges to the start of the range', () => {
expect(browserslist('iOS 15')).toEqual(['ios_saf 15.0-15.1']);
assert.deepStrictEqual(browserslist('iOS 15'), ['ios_saf 15.0-15.1']);

process.env.BROWSERSLIST = 'iOS 15';

expect(targetRuntimes()).toEqual([{ name: 'safari_ios', version: '15.0' }]);
assert.deepStrictEqual(targetRuntimes(), [{ name: 'safari_ios', version: '15.0' }]);
});

0 comments on commit 76d76a1

Please sign in to comment.