Skip to content

Commit

Permalink
test: migrate from mocha to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
remarkablemark committed Oct 15, 2023
1 parent eeb06d7 commit fd1c9d1
Show file tree
Hide file tree
Showing 13 changed files with 3,520 additions and 1,735 deletions.
4 changes: 1 addition & 3 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"env": {
"browser": true,
"commonjs": true,
"mocha": true,
"jest": true,
"node": true
},
"extends": "eslint:recommended",
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ jobs:
run: npm run lint:dts

- name: Run unit tests
run: npm run test:coverage

- name: Run ES modules tests
run: npm run test:module

- name: Build bundle
run: npm run build
run: npm run test:ci

- name: Codecov
uses: codecov/codecov-action@v3

- name: Run module tests
run: npm run test:esm

- name: Build package
run: npm run build
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run test:coverage
npm run test:ci
npx lint-staged
4 changes: 0 additions & 4 deletions .nycrc.json

This file was deleted.

8 changes: 4 additions & 4 deletions test/data.js → __tests__/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ const cases = [
['content: "Lorem ipsum";', { content: '"Lorem ipsum"' }],
['content: "foo: bar;";', { content: '"foo: bar;"' }],
[
('background-image: url("http://cdn.example.com/image.png?v=42");',
'background-image: url("http://cdn.example.com/image.png?v=42");',
{
'background-image': 'url("http://cdn.example.com/image.png?v=42")'
})
}
],
[
'background: #123456 url("https://foo.bar/image.png?v=2")',
Expand All @@ -74,13 +74,13 @@ const cases = [

// property prefix
[
('-webkit-hyphens: auto; -moz-hyphens: auto; -ms-hyphens: auto; hyphens: auto;',
'-webkit-hyphens: auto; -moz-hyphens: auto; -ms-hyphens: auto; hyphens: auto;',
{
'-webkit-hyphens': 'auto',
'-moz-hyphens': 'auto',
'-ms-hyphens': 'auto',
hyphens: 'auto'
})
}
],

// value prefix
Expand Down
54 changes: 54 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const inlineStyleParser = require('inline-style-parser');
const { cases, errors, invalids } = require('./fixtures');
const parse = require('..');

describe('valid cases', () => {
describe.each(cases)('when style=%p', (style, expected) => {
it(`returns \`${JSON.stringify(expected)}\``, () => {
expect(parse(style)).toEqual(expected);
});
});
});

describe('error cases', () => {
describe.each(errors)('when style=%p', (style) => {
it('throws error', () => {
expect(() => {
parse(style);
}).toThrow(Error);
});
});
});

describe('invalid cases', () => {
describe.each(invalids)('when style=%p', (style) => {
it('returns null', () => {
expect(parse(style)).toBe(null);
});
});
});

describe('iterator', () => {
it('returns null', () => {
expect(parse('color: #foo;', () => {})).toBe(null);
});

it('invokes callback with arguments=[name, value, declaration]', () => {
const style = 'color: #f00;';
parse(style, (name, value, declaration) => {
expect(name).toBe('color');
expect(value).toBe('#f00');
expect(declaration).toEqual(inlineStyleParser(style)[0]);
});
});

it('parses comment', () => {
const style = '/* color: #f00; */';
parse(style, (name, value, comment) => {
expect(name).toBe(undefined);
expect(value).toBe(undefined);
expect(comment.comment).toBe(' color: #f00; ');
expect(comment).toEqual(inlineStyleParser(style)[0]);
});
});
});
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @type {import('jest').Config} */
const config = {
collectCoverage: true,
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100
}
},
modulePathIgnorePatterns: ['fixtures', 'types'],
testEnvironment: 'node'
};

module.exports = config;
Loading

0 comments on commit fd1c9d1

Please sign in to comment.