Skip to content

Commit

Permalink
Throws error on NoWorkResult.root access
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Jan 30, 2022
1 parent 700c849 commit 2035231
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
9 changes: 6 additions & 3 deletions lib/no-work-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ class NoWorkResult {
this.error = error
}

this._root = root

return root
if (this.error) {
throw this.error
} else {
this._root = root
return root
}
}

get messages() {
Expand Down
4 changes: 2 additions & 2 deletions test/lazy-result.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mozilla from 'source-map-js'
import { test } from 'uvu'
import { is, equal, type } from 'uvu/assert'
import { test } from 'uvu'
import mozilla from 'source-map-js'

import LazyResult from '../lib/lazy-result.js'
import Processor from '../lib/processor.js'
Expand Down
12 changes: 3 additions & 9 deletions test/no-work-result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { test } from 'uvu'
import mozilla from 'source-map-js'
import { spy } from 'nanospy'

import { CssSyntaxError } from '../lib/postcss.js'
import NoWorkResult from '../lib/no-work-result.js'
import Processor from '../lib/processor.js'

Expand All @@ -29,7 +28,9 @@ test('has sync() method', () => {
test('throws error on sync()', () => {
let noWorkResult = new NoWorkResult(processor, 'a {', { from: '/a.css' })

noWorkResult.root
try {
noWorkResult.root
} catch {}

throws(() => noWorkResult.sync(), 'AAA')
})
Expand All @@ -43,13 +44,6 @@ test('returns cached root on second access', async () => {
not.throws(() => result.sync())
})

test('contains css syntax errors', () => {
let result = new NoWorkResult(processor, 'a {', { from: '/a.css' })
result.root
// @ts-expect-error
instance(result.error, CssSyntaxError)
})

test('contains css', () => {
let result = new NoWorkResult(processor, 'a {}', { from: '/a.css' })
is(result.css, 'a {}')
Expand Down
10 changes: 9 additions & 1 deletion test/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,9 @@ test('without plugins parses CSS only on root access', async () => {
test('catches error with empty processor', async () => {
let noWorkResult = new Processor().process('a {')

noWorkResult.root
try {
noWorkResult.root
} catch {}

let err = await catchError(async () => await noWorkResult)

Expand All @@ -547,6 +549,12 @@ test('catches error with empty processor', async () => {
instance(err, CssSyntaxError)
})

test('throws an error on root access on no plugins mode', () => {
throws(() => {
postcss().process('// invalid', { from: 'a' }).root
}, 'Unknown word')
})

test('supports plugins returning processors', () => {
let warn = spyOn(console, 'warn', () => {})
let a = (): void => {}
Expand Down

0 comments on commit 2035231

Please sign in to comment.