Skip to content

Commit

Permalink
Use Jest to test date-fns in Node.js (#1192) (closes #875; closes #1066)
Browse files Browse the repository at this point in the history
* Setup Jest
* Update Babel and Power Assert
* Make test karma suite work
* Fix multiple it used instead of describe
* Fix accidentally disabled tests
* Upgrade Sinon
* Add Node.js suite to Travis CI
* Remove node_modules from .flowconfig ignore
* Remove console.log from a test
* Use regexp for exception assertion to make it work in any Node.js
* Disable Sauce Connect because it gives me headaches
  • Loading branch information
kossnocorp authored and codinsonn committed Jun 9, 2019
1 parent 8b8952a commit 98674a6
Show file tree
Hide file tree
Showing 75 changed files with 6,542 additions and 4,372 deletions.
5 changes: 4 additions & 1 deletion .babelrc
@@ -1,4 +1,7 @@
{
"presets": ["es2015", "babel-preset-power-assert"],
"presets": [
["@babel/preset-env", { "modules": "commonjs" }],
"babel-preset-power-assert"
],
"plugins": ["add-module-exports"]
}
1 change: 0 additions & 1 deletion .flowconfig
@@ -1,5 +1,4 @@
[ignore]
.*node_modules*
.*examples*
[options]
suppress_comment= \\(.\\|\n\\)*\\$ExpectedMistake
1 change: 1 addition & 0 deletions .tool-versions
@@ -0,0 +1 @@
nodejs 10.15.3
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -12,6 +12,7 @@ env:
- TEST_SUITE=main
- TEST_SUITE=tz TZ_LENGTH=2 TZ_INDEX=0
- TEST_SUITE=tz TZ_LENGTH=2 TZ_INDEX=1
- TEST_SUITE=node

before_install:
- export CHROME_BIN=chromium-browser
Expand Down Expand Up @@ -39,4 +40,4 @@ cache: yarn

addons:
chrome: stable
sauce_connect: true
# sauce_connect: true
6 changes: 3 additions & 3 deletions config/karma.js
Expand Up @@ -148,8 +148,8 @@ function config(config) {
'karma-mocha-reporter',
'karma-phantomjs-launcher',
'karma-chrome-launcher',
'karma-sauce-launcher',
'karma-sinon',
// TODO: Make it work
// 'karma-sauce-launcher',
'karma-sourcemap-loader',
'karma-webpack',
'karma-benchmark',
Expand All @@ -172,7 +172,7 @@ function getFrameworksConfig() {
if (process.env.TEST_BENCHMARK) {
return ['benchmark']
} else {
return ['mocha', 'sinon']
return ['mocha']
}
}

Expand Down
5 changes: 5 additions & 0 deletions jest.config.js
@@ -0,0 +1,5 @@
module.exports = {
verbose: false,
setupFiles: ['jest-plugin-context/setup'],
testPathIgnorePatterns: ['<rootDir>/test.js']
}
19 changes: 10 additions & 9 deletions package.json
Expand Up @@ -39,13 +39,13 @@
},
"dependencies": {},
"devDependencies": {
"babel": "^6.5.2",
"babel-cli": "^6.6.5",
"babel-eslint": "^8.2.1",
"babel-loader": "7.1.5",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.6.0",
"babel-preset-power-assert": "^1.0.0",
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/node": "^7.4.5",
"@babel/preset-env": "^7.4.5",
"babel-loader": "8.0.6",
"babel-plugin-add-module-exports": "^1.0.2",
"babel-preset-power-assert": "^3.0.0",
"cloc": "^2.2.0",
"coveralls": "^3.0.2",
"firebase": "^3.7.1",
Expand All @@ -55,6 +55,8 @@
"gzip-size-cli": "^1.0.0",
"husky": "^1.0.0-rc.7",
"istanbul-instrumenter-loader": "^3.0.1",
"jest": "^24.8.0",
"jest-plugin-context": "^2.9.0",
"js-beautify": "^1.5.10",
"jsdoc-to-markdown": "leshakoss/jsdoc-to-markdown",
"karma": "^3.0.0",
Expand All @@ -68,7 +70,6 @@
"karma-mocha-reporter": "^2.2.5",
"karma-phantomjs-launcher": "^1.0.4",
"karma-sauce-launcher": "^1.2.0",
"karma-sinon": "^1.0.4",
"karma-sourcemap-loader": "^0.3.5",
"karma-webpack": "^4.0.0-rc.5",
"lint-staged": "^7.1.1",
Expand All @@ -80,7 +81,7 @@
"node-fetch": "^1.3.3",
"power-assert": "^1.3.1",
"prettier": "^1.14.3",
"sinon": "^1.17.3",
"sinon": "^7.3.2",
"size-limit": "^0.21.0",
"snazzy": "^7.0.0",
"systemjs": "^0.19.39",
Expand Down
21 changes: 21 additions & 0 deletions scripts/test/node.sh
@@ -0,0 +1,21 @@
#!/bin/bash

# The script runs the test suite using Jest in different Node.js versions.
#
# It's a part of the test process.

set -ex

export PATH="$(yarn bin):$PATH"

# Update and source nvm
curl -o ~/.nvm/nvm.sh https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/nvm.sh
source ~/.nvm/nvm.sh

for version in 8 9 10 11 12
do
echo "Running tests using Node.js $version"
nvm install $version
npm rebuild
jest
done
4 changes: 4 additions & 0 deletions scripts/test/travis.sh
Expand Up @@ -36,6 +36,10 @@ then
yarn test-cross-browser
env TEST_CROSS_BROWSER=true yarn test --single-run

elif [ "$TEST_SUITE" == "node" ]
then
./scripts/test/node.sh

else
printf "\n\033[0;31m" "UNKNOWN SUITE!" "\033[0m\n"
exit 1
Expand Down
4 changes: 2 additions & 2 deletions src/_lib/protectedTokens/index.js
Expand Up @@ -12,11 +12,11 @@ export function isProtectedWeekYearToken(token) {
export function throwProtectedError(token) {
if (token === 'YYYY') {
throw new RangeError(
'Use `yyyy` instead of `YYYY` for formating years; see: https://git.io/fxCyr'
'Use `yyyy` instead of `YYYY` for formatting years; see: https://git.io/fxCyr'
)
} else if (token === 'YY') {
throw new RangeError(
'Use `yy` instead of `YY` for formating years; see: https://git.io/fxCyr'
'Use `yy` instead of `YY` for formatting years; see: https://git.io/fxCyr'
)
} else if (token === 'D') {
throw new RangeError(
Expand Down
8 changes: 4 additions & 4 deletions src/format/index.js
Expand Up @@ -317,10 +317,10 @@ var unescapedLatinCharacterRegExp = /[a-zA-Z]/
* @throws {RangeError} `options.locale` must contain `formatLong` property
* @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
* @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
* @throws {RangeError} use `yyyy` instead of `YYYY` for formating years; see: https://git.io/fxCyr
* @throws {RangeError} use `yy` instead of `YY` for formating years; see: https://git.io/fxCyr
* @throws {RangeError} use `d` instead of `D` for formating days of the month; see: https://git.io/fxCyr
* @throws {RangeError} use `dd` instead of `DD` for formating days of the month; see: https://git.io/fxCyr
* @throws {RangeError} use `yyyy` instead of `YYYY` for formatting years; see: https://git.io/fxCyr
* @throws {RangeError} use `yy` instead of `YY` for formatting years; see: https://git.io/fxCyr
* @throws {RangeError} use `d` instead of `D` for formatting days of the month; see: https://git.io/fxCyr
* @throws {RangeError} use `dd` instead of `DD` for formatting days of the month; see: https://git.io/fxCyr
* @throws {RangeError} format string contains an unescaped latin alphabet character
*
* @example
Expand Down
8 changes: 4 additions & 4 deletions src/format/test.js
Expand Up @@ -738,7 +738,7 @@ describe('format', function() {
assert.throws(block, RangeError)
assert.throws(
block,
'Use `d` instead of `D` for formatting days of the month; see: https://git.io/fxCyr'
/Use `d` instead of `D` for formatting days of the month; see: https:\/\/git.io\/fxCyr/
)
})

Expand All @@ -754,7 +754,7 @@ describe('format', function() {
assert.throws(block, RangeError)
assert.throws(
block,
'Use `dd` instead of `DD` for formatting days of the month; see: https://git.io/fxCyr'
/Use `dd` instead of `DD` for formatting days of the month; see: https:\/\/git.io\/fxCyr/
)
})

Expand All @@ -770,7 +770,7 @@ describe('format', function() {
assert.throws(block, RangeError)
assert.throws(
block,
'Use `yy` instead of `YY` for formating years; see: https://git.io/fxCyr'
/Use `yy` instead of `YY` for formatting years; see: https:\/\/git.io\/fxCyr/
)
})

Expand All @@ -786,7 +786,7 @@ describe('format', function() {
assert.throws(block, RangeError)
assert.throws(
block,
'Use `yyyy` instead of `YYYY` for formating years; see: https://git.io/fxCyr'
/Use `yyyy` instead of `YYYY` for formatting years; see: https:\/\/git.io\/fxCyr/
)
})

Expand Down
45 changes: 25 additions & 20 deletions src/isDate/test.js
Expand Up @@ -5,35 +5,38 @@
import assert from 'power-assert'
import isDate from '.'

describe('isDate', function () {
it('returns true if the given value is a date object', function () {
describe('isDate', function() {
it('returns true if the given value is a date object', function() {
assert(isDate(new Date()))
})

it('returns true if the given value is an Invalid Date', function () {
it('returns true if the given value is an Invalid Date', function() {
assert(isDate(new Date(NaN)))
})

context('with date passed from another iframe', function () {
afterEach(function () {
context('with date passed from another iframe', function() {
afterEach(function() {
const iframe = document.getElementById('iframe')
iframe && iframe.remove()
})

it('returns true for a date passed from another iframe', function (done) {
const iframe = document.createElement('iframe')
iframe.id = 'iframe'
iframe.addEventListener('load', function () {
execScript('window.date = new Date()') // eslint-disable-line no-implied-eval
assert(isDate(iframe.contentWindow.date))
// $ExpectedMistake sadly, but Flow doesn't know about Mocha's done
done()
// jsdom is sad
if (!process.env.JEST_WORKER_ID) {
it('returns true for a date passed from another iframe', function(done) {
const iframe = document.createElement('iframe')
iframe.id = 'iframe'
iframe.addEventListener('load', function() {
execScript('window.date = new Date()') // eslint-disable-line no-implied-eval
assert(isDate(iframe.contentWindow.date))
// $ExpectedMistake sadly, but Flow doesn't know about Mocha's done
done()
})
if (!document.body) throw new Error('document.body is not defined')
document.body.appendChild(iframe)
})
if (!document.body) throw new Error('document.body is not defined')
document.body.appendChild(iframe)
})
}

function execScript (scriptText) {
function execScript(scriptText) {
const iframe = document.querySelector('iframe#iframe')
if (!iframe || !(iframe instanceof HTMLIFrameElement)) {
throw new Error("Can't execute the script because iframe isn't found")
Expand All @@ -42,21 +45,23 @@ describe('isDate', function () {
const script = doc.createElement('script')
script.innerText = scriptText
if (!(doc.body instanceof iframe.contentWindow.HTMLBodyElement)) {
throw new Error("Can't execute the script because iframe does not have body")
throw new Error(
"Can't execute the script because iframe does not have body"
)
}
doc.body.append(script)
}
})

it('returns false if the given value is not a date object', function () {
it('returns false if the given value is not a date object', function() {
assert(!isDate(new Date().getTime()))
assert(!isDate(new Date().toISOString()))
assert(!isDate({}))
assert(!isDate(null))
assert(!isDate(0))
})

it('throws TypeError exception if passed less than 1 argument', function () {
it('throws TypeError exception if passed less than 1 argument', function() {
assert.throws(isDate.bind(null), TypeError)
})
})
2 changes: 1 addition & 1 deletion src/locale/af/test.js
Expand Up @@ -121,7 +121,7 @@ describe('af locale', function() {
assert(result === 'middernag, middernag, middernag')
})

it('flexible day periods', function() {
describe('flexible day periods', function() {
it('works as expected', function() {
var result = format(date, 'h B', { locale: locale })
assert(result === '10 uur die oggend')
Expand Down
2 changes: 1 addition & 1 deletion src/locale/ar-DZ/test.js
Expand Up @@ -123,7 +123,7 @@ describe('ar-DZ locale', function() {
assert(result === 'نصف الليل, نصف الليل, ن')
})

it('flexible day periods', function() {
describe('flexible day periods', function() {
it('works as expected', function() {
var result = format(date, 'h B', { locale: locale })
assert(result === '10 في الصباح')
Expand Down
2 changes: 1 addition & 1 deletion src/locale/ar-SA/test.js
Expand Up @@ -123,7 +123,7 @@ describe('ar-SA locale', function() {
assert(result === 'نصف الليل, نصف الليل, ن')
})

it('flexible day periods', function() {
describe('flexible day periods', function() {
it('works as expected', function() {
var result = format(date, 'h B', { locale: locale })
assert(result === '10 في الصباح')
Expand Down
8 changes: 5 additions & 3 deletions src/locale/ar/test.js
@@ -1,9 +1,11 @@
// @flow
/* eslint-env mocha */

// import assert from 'power-assert'
import assert from 'power-assert'
// import locale from '.'

describe.skip('ar locale', function () {

describe('ar locale', function() {
it('hopes to get tests one day', () => {
assert('hope')
})
})
4 changes: 2 additions & 2 deletions src/locale/be/test.js
Expand Up @@ -173,10 +173,10 @@ describe('be locale', function() {
assert(result === 'поўн., поўнач, поўн.')
})

it('flexible day periods', function() {
describe('flexible day periods', function() {
it('works as expected', function() {
var result = format(date, 'h B', { locale: locale })
assert(result === '10 раніцы')
assert(result === '10 ран.')
})
})
})
Expand Down
8 changes: 5 additions & 3 deletions src/locale/bg/test.js
@@ -1,9 +1,11 @@
// @flow
/* eslint-env mocha */

// import assert from 'power-assert'
import assert from 'power-assert'
// import locale from '.'

describe.skip('bg locale', function () {

describe('bg locale', function() {
it('hopes to get tests one day', () => {
assert('hope')
})
})
2 changes: 1 addition & 1 deletion src/locale/ca/test.js
Expand Up @@ -121,7 +121,7 @@ describe('ca locale', function() {
assert(result === 'mitjanit, mitjanit, mn')
})

it('flexible day periods', function() {
describe('flexible day periods', function() {
it('works as expected', function() {
var result = format(date, 'h B', { locale: locale })
assert(result === '10 del matí')
Expand Down
8 changes: 5 additions & 3 deletions src/locale/cs/test.js
@@ -1,9 +1,11 @@
// @flow
/* eslint-env mocha */

// import assert from 'power-assert'
import assert from 'power-assert'
// import locale from '.'

describe.skip('cs locale', function () {

describe('cs locale', function() {
it('hopes to get tests one day', () => {
assert('hope')
})
})
3 changes: 1 addition & 2 deletions src/locale/da/test.js
Expand Up @@ -121,7 +121,7 @@ describe('da locale', function() {
assert(result === 'midnat, midnat, midnat')
})

it('flexible day periods', function() {
describe('flexible day periods', function() {
it('works as expected', function() {
var result = format(date, 'h B', { locale: locale })
assert(result === '10 om morgenen')
Expand Down Expand Up @@ -374,7 +374,6 @@ describe('da locale', function() {

it('abbreviated', function() {
var result = parse('nov', 'MMM', baseDate, { locale: locale })
console.log(parse('feb', 'MMM', baseDate, { locale: locale }))
assert.deepEqual(result, new Date(1986, 10 /* Nov */, 1))
})

Expand Down

0 comments on commit 98674a6

Please sign in to comment.