Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Apr 26, 2021
1 parent 2b1d04c commit 8484d27
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 47 deletions.
36 changes: 21 additions & 15 deletions test/css-syntax-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import postcss, {
Rule
} from '../lib/postcss.js'

async function catchError(cb: () => Promise<any>): Promise<CssSyntaxError> {
try {
await cb()
} catch (e) {
if (e.name !== 'CssSyntaxError') throw e
return e
}
throw new Error('Error was not thrown')
}

function parseError(
css: string,
opts?: Pick<ProcessOptions, 'map' | 'from'>
Expand Down Expand Up @@ -219,7 +229,7 @@ it('set source plugin', () => {
)
})

it('set source plugin automatically', () => {
it('set source plugin automatically', async () => {
let plugin: Plugin = {
postcssPlugin: 'test-plugin',
Once(css) {
Expand All @@ -229,16 +239,14 @@ it('set source plugin automatically', () => {
}
}

return postcss([plugin])
.process('a{}')
.catch(error => {
if (error.name !== 'CssSyntaxError') throw error
expect(error.plugin).toEqual('test-plugin')
expect(error.toString()).toMatch(/test-plugin/)
})
let error = await catchError(() =>
postcss([plugin]).process('a{}', { from: undefined })
)
expect(error.plugin).toEqual('test-plugin')
expect(error.toString()).toMatch(/test-plugin/)
})

it('set plugin automatically in async', () => {
it('set plugin automatically in async', async () => {
let plugin: Plugin = {
postcssPlugin: 'async-plugin',
Once(css) {
Expand All @@ -250,10 +258,8 @@ it('set plugin automatically in async', () => {
}
}

return postcss([plugin])
.process('a{}')
.catch(error => {
if (error.name !== 'CssSyntaxError') throw error
expect(error.plugin).toEqual('async-plugin')
})
let error = await catchError(() =>
postcss([plugin]).process('a{}', { from: undefined })
)
expect(error.plugin).toEqual('async-plugin')
})
60 changes: 28 additions & 32 deletions test/processor.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { resolve as pathResolve } from 'path'
import { delay } from 'nanodelay'

import postcss, {
Plugin,
Expand Down Expand Up @@ -27,6 +28,15 @@ function getCalls(func: any): any {
return func.mock.calls
}

async function catchError(cb: () => Promise<any>): Promise<Error> {
try {
await cb()
} catch (e) {
return e
}
throw new Error('Error was not thrown')
}

let beforeFix = new Processor([
(root: Root) => {
root.walkRules(rule => {
Expand Down Expand Up @@ -277,54 +287,40 @@ it('runs async plugin only once', async () => {
expect(calls).toEqual(1)
})

it('supports async errors', () => {
it('supports async errors', async () => {
let error = new Error('Async')
let async = (): Promise<void> => {
return new Promise<void>((resolve, reject) => {
reject(error)
})
}
return new Promise<void>((resolve, reject) => {
let result = new Processor([async]).process('', { from: undefined })
result
.then(() => {
reject(new Error('Called .then in failed processing'))
})
.catch(err => {
expect(err).toEqual(error)
return result.catch(err2 => {
expect(err2).toEqual(error)
resolve()
})
})
let result = new Processor([async]).process('', { from: undefined })
let err1 = await catchError(async () => await result)
expect(err1).toEqual(error)

let err2: Error | undefined
result.catch(catched => {
err2 = catched
})
await delay(10)
expect(err2).toEqual(error)
})

it('supports sync errors in async mode', () => {
it('supports sync errors in async mode', async () => {
let error = new Error('Async')
let async = (): void => {
throw error
}
return new Promise<void>((resolve, reject) => {
new Processor([async])
.process('', { from: undefined })
.then(() => {
reject(new Error('Called .then in failed processing'))
})
.catch(err => {
expect(err).toEqual(error)
resolve()
})
})
let err = await catchError(() =>
new Processor([async]).process('', { from: undefined })
)
expect(err).toEqual(error)
})

it('throws parse error in async', async () => {
let err
try {
await new Processor([() => {}]).process('a{', { from: undefined })
} catch (e) {
err = e
}
let err = await catchError(() =>
new Processor([() => {}]).process('a{', { from: undefined })
)
expect(err.message).toEqual('<css input>:1:1: Unclosed block')
})

Expand Down

0 comments on commit 8484d27

Please sign in to comment.