Skip to content

Commit

Permalink
fix: if preprocessor is async function and doesn't return a content t…
Browse files Browse the repository at this point in the history
…hen await donePromise
  • Loading branch information
anthony-redFox committed Oct 18, 2019
1 parent 1cd87ad commit e55a258
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ function executeProcessor (process, file, content) {
}
}
})
return process(content, file, done) || donePromise

return (process(content, file, done) || Promise.resolve()).then((content) => {
if (content) {
// async process correctly returned content
return content
}
// process called done() (Either old sync api or an async function that did not return content)
return donePromise
})
}

async function runProcessors (preprocessors, file, content) {
Expand Down
42 changes: 42 additions & 0 deletions test/unit/preprocessor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,48 @@ describe('preprocessor', () => {
})
})

it('should get content if preprocessor is an async function or return Promise with content', (done) => {
const fakePreprocessor = sinon.spy(async (content, file, done) => {
file.path = file.path + '-preprocessed'
return 'new-content'
})

const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)

const file = { originalPath: '/some/.dir/a.js', path: 'path' }

pp(file, () => {
expect(fakePreprocessor).to.have.been.called
expect(file.path).to.equal('path-preprocessed')
expect(file.content).to.equal('new-content')
done()
})
})

it('should get content if preprocessor is an async function still calling done()', (done) => {
const fakePreprocessor = sinon.spy(async (content, file, done) => {
file.path = file.path + '-preprocessed'
done(null, 'new-content')
})

const injector = new di.Injector([{
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
}, emitterSetting])
pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)

const file = { originalPath: '/some/.dir/a.js', path: 'path' }

pp(file, () => {
expect(fakePreprocessor).to.have.been.called
expect(file.path).to.equal('path-preprocessed')
expect(file.content).to.equal('new-content')
done()
})
})

it('should check patterns after creation when invoked', (done) => {
const fakePreprocessor = sinon.spy((content, file, done) => {
file.path = file.path + '-preprocessed'
Expand Down

0 comments on commit e55a258

Please sign in to comment.