Skip to content

Commit

Permalink
use jest instead of ava (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsantiago committed Aug 28, 2020
1 parent f8e291b commit 24b1c3d
Show file tree
Hide file tree
Showing 11 changed files with 9,045 additions and 4,178 deletions.
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
}
}
}
13,037 changes: 8,958 additions & 4,079 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
"main": "./dist/subtitle.bundle.js",
"module": "./lib/index.js",
"scripts": {
"test": "npm run lint && npm run ava",
"ava": "npm run build && ava test/*.test.js",
"test": "jest",
"test:coverage": "jest --coverage",
"lint": "standard subtitle.js lib/*.js test/*.test.js",
"coverage": "nyc npm test",
"report": "nyc report --reporter=html",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"coveralls": "jest --coverage && coveralls < coverage/lcov.info",
"build": "webpack"
},
"keywords": [
Expand All @@ -35,7 +33,7 @@
"parser": "babel-eslint"
},
"devDependencies": {
"ava": "^0.17.0",
"@babel/plugin-transform-modules-commonjs": "^7.10.4",
"babel-core": "^6.26.0",
"babel-eslint": "^7.2.3",
"babel-loader": "^7.1.2",
Expand All @@ -44,7 +42,7 @@
"coveralls": "^2.11.15",
"fs-promise": "^1.0.0",
"glob-contents": "0.0.3",
"nyc": "^10.0.0",
"jest": "^26.4.2",
"pify": "^3.0.0",
"standard": "^8.0.0",
"webpack": "^3.11.0"
Expand Down
35 changes: 17 additions & 18 deletions test/parse.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import test from 'ava'
import fs from 'fs'
import path from 'path'
import promisify from 'pify'
import glob from 'glob-contents'
import { parse } from '..'
import { parse } from '../lib'

const readFile = promisify(fs.readFile)

test('parse all examples', async t => {
test('parse all examples', async () => {
const srt = await glob(path.join(__dirname, '/examples/*.srt'))

await Promise.all(
Object.keys(srt).map(async filepath => {
const basename = path.basename(filepath, '.srt')
const json = await readFile(path.join(__dirname, `/examples/${basename}.json`), 'utf8')
const subtitles = JSON.parse(json)
t.deepEqual(parse(srt[filepath]), subtitles)
expect(parse(srt[filepath])).toEqual(subtitles)
})
)
})

test('parse SRT captions', t => {
test('parse SRT captions', () => {
const srt = `
1
02:12:34,647 --> 02:12:35,489
Expand Down Expand Up @@ -55,10 +54,10 @@ Welcome to the Planet.
}
]

t.deepEqual(value, expected)
expect(value).toEqual(expected)
})

test('parse VTT captions', t => {
test('parse VTT captions', () => {
const vtt = `
WEBVTT - Test VTT cues
Expand Down Expand Up @@ -96,10 +95,10 @@ Welcome to the Planet.
}
]

t.deepEqual(value, expected)
expect(value).toEqual(expected)
})

test('parse VTT caption with headers', t => {
test('parse VTT caption with headers', () => {
const vtt = `
WEBVTT - Test VTT cues
Kind: captions
Expand Down Expand Up @@ -139,14 +138,14 @@ Welcome to the Planet.
}
]

t.deepEqual(value, expected)
expect(value).toEqual(expected)
})

test('it should return an empty array', t => {
t.deepEqual(parse(), [])
test('it should return an empty array', () => {
expect(parse()).toEqual([])
})

test('parse 00:00:00,000 caption', t => {
test('parse 00:00:00,000 caption', () => {
const srt = `
1
00:00:00,000 --> 00:00:00,100
Expand All @@ -161,10 +160,10 @@ Hi.
}
]

t.deepEqual(value, expected)
expect(value).toEqual(expected)
})

test('parse text that contains only empty space', t => {
test('parse text that contains only empty space', () => {
const srt = `
1
00:00:00,000 --> 00:00:00,100
Expand All @@ -187,10 +186,10 @@ Hi.`
}
]

t.deepEqual(value, expected)
expect(value).toEqual(expected)
})

test('parse separated texts', t => {
test('parse separated texts', () => {
const srt = `
1
00:00:00,000 --> 00:00:00,100
Expand All @@ -214,5 +213,5 @@ Who else could be trusted?`
}
]

t.deepEqual(value, expected)
expect(value).toEqual(expected)
})
20 changes: 5 additions & 15 deletions test/parseTimestamps.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import test from 'ava'
import { parseTimestamps } from '..'
import { parseTimestamps } from '../lib'

const checkWith = (t) => (val, expected) => {
t.deepEqual(parseTimestamps(val), expected)
}
const check = (val, expected) => expect(parseTimestamps(val)).toEqual(expected)

test('parseTimestamps with SRT input', t => {
const check = checkWith(t)

// SRT timestamps
test('parseTimestamps with SRT input', () => {
check('00:00:20,000 --> 00:00:24,400', {
start: 20000,
end: 24400
Expand Down Expand Up @@ -40,9 +34,7 @@ test('parseTimestamps with SRT input', t => {
})
})

test('parseTimestamps with VTT input and short formats', t => {
const check = checkWith(t)

test('parseTimestamps with VTT input and short formats', () => {
check('00:20.000 --> 00:24.400', {
start: 20000,
end: 24400
Expand All @@ -64,9 +56,7 @@ test('parseTimestamps with VTT input and short formats', t => {
})
})

test('parseTimestamps with VTT settings', t => {
const check = checkWith(t)

test('parseTimestamps with VTT settings', () => {
check('12:34:56,789 --> 98:76:54,321 align:middle line:90%', {
start: 45296789,
end: 357414321,
Expand Down
21 changes: 10 additions & 11 deletions test/resync.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import test from 'ava'
import { resync } from '..'
import { resync } from '../lib'

const captions = [
{
Expand Down Expand Up @@ -37,7 +36,7 @@ const captionsMS = [
}
]

test('delay 100ms', t => {
test('delay 100ms', () => {
const expected = {
start: 10000,
end: 10400,
Expand All @@ -47,11 +46,11 @@ test('delay 100ms', t => {
const result = resync(captions, -100)[0]
const result2 = resync(captionsMS, -100)[0]

t.deepEqual(result, expected)
t.deepEqual(result2, expected)
expect(result).toEqual(expected)
expect(result2).toEqual(expected)
})

test('advance 1s', t => {
test('advance 1s', () => {
const expected = {
start: 21650,
end: 24300,
Expand All @@ -61,11 +60,11 @@ test('advance 1s', t => {
const result = resync(captions, 1000)[1]
const result2 = resync(captionsMS, 1000)[1]

t.deepEqual(result, expected)
t.deepEqual(result2, expected)
expect(result).toEqual(expected)
expect(result2).toEqual(expected)
})

test('delay 2 hours', t => {
test('delay 2 hours', () => {
const expected = {
start: 180000,
end: 190150,
Expand All @@ -75,6 +74,6 @@ test('delay 2 hours', t => {
const result = resync(captions, 2 * 60 * 1000 * -1)[2]
const result2 = resync(captionsMS, 2 * 60 * 1000 * -1)[2]

t.deepEqual(result, expected)
t.deepEqual(result2, expected)
expect(result).toEqual(expected)
expect(result2).toEqual(expected)
})
11 changes: 5 additions & 6 deletions test/stringify.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import test from 'ava'
import fs from 'fs'
import path from 'path'
import promisify from 'pify'
import glob from 'glob-contents'
import { stringify } from '..'
import { stringify } from '../lib'

const readFile = promisify(fs.readFile)

test('stringify all examples', async t => {
test('stringify all examples', async () => {
const srt = await glob(path.join(__dirname, '/examples/*.srt'))

await Promise.all(
Expand All @@ -21,12 +20,12 @@ test('stringify all examples', async t => {
.replace(/\r\n/g, '\n')
.replace(/\n{3,}/g, '\n\n')

t.deepEqual(stringify(subtitles), normalizedSrt)
expect(stringify(subtitles)).toEqual(normalizedSrt)
})
)
})

test('stringify captions with timestamp in SRT format', t => {
test('stringify captions with timestamp in SRT format', () => {
const captions = [
{
start: 7954647,
Expand Down Expand Up @@ -59,5 +58,5 @@ Lois Lane.
Welcome to the Planet.
`.trim().concat('\n')

t.is(stringify(captions), expected)
expect(stringify(captions)).toBe(expected)
})
11 changes: 5 additions & 6 deletions test/stringifyVtt.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import test from 'ava'
import fs from 'fs'
import path from 'path'
import promisify from 'pify'
import glob from 'glob-contents'
import { stringifyVtt } from '..'
import { stringifyVtt } from '../lib'

const readFile = promisify(fs.readFile)

test('stringify all examples', async t => {
test('stringify all examples', async () => {
const vtt = await glob(path.join(__dirname, '/examples/*.vtt'))

await Promise.all(
Expand All @@ -21,12 +20,12 @@ test('stringify all examples', async t => {
.replace(/\r\n/g, '\n')
.replace(/\n{3,}/g, '\n\n')

t.deepEqual(stringifyVtt(subtitles), normalizedVtt)
expect(stringifyVtt(subtitles)).toEqual(normalizedVtt)
})
)
})

test('stringify captions with timestamp in WebVTT format', t => {
test('stringify captions with timestamp in WebVTT format', () => {
const captions = [
{
start: 940647,
Expand Down Expand Up @@ -61,5 +60,5 @@ Lois Lane.
Welcome to the Planet.
`.trim().concat('\n')

t.is(stringifyVtt(captions), expected)
expect(stringifyVtt(captions)).toBe(expected)
})
35 changes: 17 additions & 18 deletions test/toMS.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import test from 'ava'
import { toMS } from '..'
import { toMS } from '../lib'

test('should convert SRT time to milliseconds', t => {
test('should convert SRT time to milliseconds', () => {
const time1 = {
srt: '00:02:22,542',
ms: 120000 + 22000 + 542
Expand All @@ -17,12 +16,12 @@ test('should convert SRT time to milliseconds', t => {
ms: 0
}

t.is(toMS(time1.srt), time1.ms)
t.is(toMS(time2.srt), time2.ms)
t.is(toMS(time3.srt), time3.ms)
expect(toMS(time1.srt)).toBe(time1.ms)
expect(toMS(time2.srt)).toBe(time2.ms)
expect(toMS(time3.srt)).toBe(time3.ms)
})

test('should convert VTT time to milliseconds including short formats', t => {
test('should convert VTT time to milliseconds including short formats', () => {
const time1 = {
srt: '02:22.542',
ms: 120000 + 22000 + 542
Expand All @@ -43,20 +42,20 @@ test('should convert VTT time to milliseconds including short formats', t => {
ms: (1201 * 3600000) + 3060000 + 58000 + 219
}

t.is(toMS(time1.srt), time1.ms)
t.is(toMS(time2.srt), time2.ms)
t.is(toMS(time3.srt), time3.ms)
t.is(toMS(time4.srt), time4.ms)
expect(toMS(time1.srt)).toBe(time1.ms)
expect(toMS(time2.srt)).toBe(time2.ms)
expect(toMS(time3.srt)).toBe(time3.ms)
expect(toMS(time4.srt)).toBe(time4.ms)
})

test('invalid format should throw an error', t => {
t.throws(function () {
test('invalid format should throw an error', () => {
expect(() => {
toMS('12,34:56,78')
})
}).toThrow()
})

test('should return the given numbers', t => {
t.is(toMS(1000), 1000)
t.is(toMS(600), 600)
t.is(toMS(-150), -150)
test('should return the given numbers', () => {
expect(toMS(1000)).toBe(1000)
expect(toMS(600)).toBe(600)
expect(toMS(-150)).toBe(-150)
})
Loading

0 comments on commit 24b1c3d

Please sign in to comment.