Skip to content

Commit

Permalink
🚚 Exchange unit and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sumolari committed Apr 2, 2022
1 parent f47a3b4 commit e877ce9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 93 deletions.
93 changes: 66 additions & 27 deletions test/index.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,82 @@
import { describe, it, expect, jest } from '@jest/globals'
import { describe, it, expect } from '@jest/globals'

import * as execa from 'execa'
import * as fs from 'fs'
import { resolve as resolvePath } from 'path'

import getDuration from '../src'

jest.mock('execa', () =>
jest.fn().mockResolvedValue({
stdout: 'duration="42.0"',
} as never)
)
jest.mock('is-stream', () => jest.fn().mockReturnValue(false))
import getDuration, { getAudioDurationInSeconds } from '../src'

const expectedOGGAudioDuration = 33
const expectedFLACAudioDuration = 97
const expectedWAVAudioDuration = 33
const expectedAudioDurationThreshold = -1

const sampleOGGFilePath = resolvePath(__dirname, './Rayman_2_music_sample.ogg')
const sampleOGGWithSpacesFilePath = resolvePath(
__dirname,
'./Rayman 2 music sample.ogg'
)
const sampleFLACFilePath = resolvePath(
__dirname,
'./2L-125_stereo-44k-16b_04.flac'
)
const sampleWAVFilePath = resolvePath(__dirname, './file_example_WAV_1MG.wav')

describe('get-audio-duration', () => {
describe('when using a file path', () => {
it('Should use overriden ffprobe when provided', async () => {
const durationPromise = getDuration(
'fake file',
'the overriden path to ffprobe'
)
it('Should export function under named export, too', () => {
expect(getDuration).toEqual(getAudioDurationInSeconds)
})

expect(execa).toHaveBeenCalledWith(
'the overriden path to ffprobe',
expect.anything()
)
const getReadableStreamAsInput = (pathToFile: string) =>
fs.createReadStream(pathToFile)
const getFilePathAsInput = (pathToFile: string) => pathToFile

await expect(durationPromise).resolves.toBeCloseTo(
42.0,
describe.each`
description | getInput | isOGGSupported
${'readable stream'} | ${getReadableStreamAsInput} | ${false}
${'file path'} | ${getFilePathAsInput} | ${true}
`('when using a $description', ({ getInput, isOGGSupported }) => {
it('Should return proper duration for flac files', async () => {
const input = getInput(sampleFLACFilePath)
const duration = await getDuration(input)
expect(duration).toBeCloseTo(
expectedFLACAudioDuration,
expectedAudioDurationThreshold
)
})
})

describe('when input is invalid', () => {
it('Should throw an error', async () => {
await expect(getDuration(42 as unknown as string)).rejects.toThrowError(
'Given input was neither a string nor a Stream'
it.skip('Should return proper duration for wav files', async () => {
const input = getInput(sampleWAVFilePath)
const duration = await getDuration(input)
expect(duration).toBeCloseTo(
expectedWAVAudioDuration,
expectedAudioDurationThreshold
)
})

it('Should throw an error if not an audio stream', async () => {
const input = getInput(resolvePath(__dirname, __filename))
const durationPromise = getDuration(input)
await expect(durationPromise).rejects.toThrowError()
})

if (isOGGSupported) {
it('Should return proper duration for OGG files', async () => {
const input = getInput(sampleOGGFilePath)
const duration = await getDuration(input)
expect(duration).toBeCloseTo(
expectedOGGAudioDuration,
expectedAudioDurationThreshold
)
})

it('Should return proper duration if file contains spaces', async () => {
const input = getInput(sampleOGGWithSpacesFilePath)
const duration = await getDuration(input)
expect(duration).toBeCloseTo(
expectedOGGAudioDuration,
expectedAudioDurationThreshold
)
})
}
})
})
93 changes: 27 additions & 66 deletions test/index.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,43 @@
import { describe, it, expect } from '@jest/globals'
import { describe, it, expect, jest } from '@jest/globals'

import * as fs from 'fs'
import { resolve as resolvePath } from 'path'
import * as execa from 'execa'

import getDuration, { getAudioDurationInSeconds } from '../src'
import getDuration from '../src'

const expectedOGGAudioDuration = 33
const expectedFLACAudioDuration = 97
const expectedWAVAudioDuration = 33
const expectedAudioDurationThreshold = -1

const sampleOGGFilePath = resolvePath(__dirname, './Rayman_2_music_sample.ogg')
const sampleOGGWithSpacesFilePath = resolvePath(
__dirname,
'./Rayman 2 music sample.ogg'
jest.mock('execa', () =>
jest.fn().mockResolvedValue({
stdout: 'duration="42.0"',
} as never)
)
const sampleFLACFilePath = resolvePath(
__dirname,
'./2L-125_stereo-44k-16b_04.flac'
)
const sampleWAVFilePath = resolvePath(__dirname, './file_example_WAV_1MG.wav')
jest.mock('is-stream', () => jest.fn().mockReturnValue(false))

describe('get-audio-duration', () => {
it('Should export function under named export, too', () => {
expect(getDuration).toEqual(getAudioDurationInSeconds)
})
const expectedAudioDurationThreshold = -1

const getReadableStreamAsInput = (pathToFile: string) =>
fs.createReadStream(pathToFile)
const getFilePathAsInput = (pathToFile: string) => pathToFile
describe('get-audio-duration', () => {
describe('when using a file path', () => {
it('Should use overriden ffprobe when provided', async () => {
const durationPromise = getDuration(
'fake file',
'the overriden path to ffprobe'
)

describe.each`
description | getInput | isOGGSupported
${'readable stream'} | ${getReadableStreamAsInput} | ${false}
${'file path'} | ${getFilePathAsInput} | ${true}
`('when using a $description', ({ getInput, isOGGSupported }) => {
it('Should return proper duration for flac files', async () => {
const input = getInput(sampleFLACFilePath)
const duration = await getDuration(input)
expect(duration).toBeCloseTo(
expectedFLACAudioDuration,
expectedAudioDurationThreshold
expect(execa).toHaveBeenCalledWith(
'the overriden path to ffprobe',
expect.anything()
)
})

it.skip('Should return proper duration for wav files', async () => {
const input = getInput(sampleWAVFilePath)
const duration = await getDuration(input)
expect(duration).toBeCloseTo(
expectedWAVAudioDuration,
await expect(durationPromise).resolves.toBeCloseTo(
42.0,
expectedAudioDurationThreshold
)
})
})

it('Should throw an error if not an audio stream', async () => {
const input = getInput(resolvePath(__dirname, __filename))
const durationPromise = getDuration(input)
await expect(durationPromise).rejects.toThrowError()
describe('when input is invalid', () => {
it('Should throw an error', async () => {
await expect(getDuration(42 as unknown as string)).rejects.toThrowError(
'Given input was neither a string nor a Stream'
)
})

if (isOGGSupported) {
it('Should return proper duration for OGG files', async () => {
const input = getInput(sampleOGGFilePath)
const duration = await getDuration(input)
expect(duration).toBeCloseTo(
expectedOGGAudioDuration,
expectedAudioDurationThreshold
)
})

it('Should return proper duration if file contains spaces', async () => {
const input = getInput(sampleOGGWithSpacesFilePath)
const duration = await getDuration(input)
expect(duration).toBeCloseTo(
expectedOGGAudioDuration,
expectedAudioDurationThreshold
)
})
}
})
})

0 comments on commit e877ce9

Please sign in to comment.