Skip to content

Commit f91be24

Browse files
anthony-redFoxjohnjbarton
authored andcommitted
fix: if preprocessor is async function and doesn't return a content then await donePromise (#3387)
1 parent 259be0d commit f91be24

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎lib/preprocessor.js‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ function executeProcessor (process, file, content) {
2424
}
2525
}
2626
})
27-
returnprocess(content,file,done)||donePromise
27+
28+
return(process(content,file,done)||Promise.resolve()).then((content)=>{
29+
if(content){
30+
// async process correctly returned content
31+
returncontent
32+
}
33+
// process called done() (Either old sync api or an async function that did not return content)
34+
returndonePromise
35+
})
2836
}
2937

3038
asyncfunctionrunProcessors(preprocessors,file,content){

‎test/unit/preprocessor.spec.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,48 @@ describe('preprocessor', () => {
8080
})
8181
})
8282

83+
it('should get content if preprocessor is an async function or return Promise with content',(done)=>{
84+
constfakePreprocessor=sinon.spy(async(content,file,done)=>{
85+
file.path=file.path+'-preprocessed'
86+
return'new-content'
87+
})
88+
89+
constinjector=newdi.Injector([{
90+
'preprocessor:fake': ['factory',function(){returnfakePreprocessor}]
91+
},emitterSetting])
92+
pp=m.createPriorityPreprocessor({'**/*.js': ['fake']},{},null,injector)
93+
94+
constfile={originalPath: '/some/.dir/a.js',path: 'path'}
95+
96+
pp(file,()=>{
97+
expect(fakePreprocessor).to.have.been.called
98+
expect(file.path).to.equal('path-preprocessed')
99+
expect(file.content).to.equal('new-content')
100+
done()
101+
})
102+
})
103+
104+
it('should get content if preprocessor is an async function still calling done()',(done)=>{
105+
constfakePreprocessor=sinon.spy(async(content,file,done)=>{
106+
file.path=file.path+'-preprocessed'
107+
done(null,'new-content')
108+
})
109+
110+
constinjector=newdi.Injector([{
111+
'preprocessor:fake': ['factory',function(){returnfakePreprocessor}]
112+
},emitterSetting])
113+
pp=m.createPriorityPreprocessor({'**/*.js': ['fake']},{},null,injector)
114+
115+
constfile={originalPath: '/some/.dir/a.js',path: 'path'}
116+
117+
pp(file,()=>{
118+
expect(fakePreprocessor).to.have.been.called
119+
expect(file.path).to.equal('path-preprocessed')
120+
expect(file.content).to.equal('new-content')
121+
done()
122+
})
123+
})
124+
83125
it('should check patterns after creation when invoked',(done)=>{
84126
constfakePreprocessor=sinon.spy((content,file,done)=>{
85127
file.path=file.path+'-preprocessed'

0 commit comments

Comments
 (0)