Last active
September 10, 2024 11:11
-
-
Save mreis1/65be70655c886f4e0de24714262b4e52 to your computer and use it in GitHub Desktop.
Mocha - Chai - Ts-node - Typescript - Mocha-Steps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"scripts": { | |
"test:xspec": "npx ts-mocha -p ./test/tsconfig.xspec.json src/**/*.xspec.ts --require mocha-steps" | |
}, | |
"devDependencies": { | |
"@types/chai": "^4.3.4", | |
"@types/mocha": "^8.0.1", | |
"@types/mocha-steps": "^1.3.0", | |
"@types/node": "^16.18.11", | |
"ts-mocha": "^10.0.0", | |
"ts-node": "^10.9.1", | |
"ts-node-dev": "^2.0.0", | |
"typescript": "^4.9.4", | |
"chai": "^4.3.7", | |
"proxyquire": "^2.1.3" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { sampleB } from './sample-b'; | |
export function sampleA(v: string) { | |
return sampleB(v+'_sampleA'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('sampleB') | |
export function sampleB(v: string) { | |
return v + '_sampleB'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { sampleA } from './sample-a'; | |
console.log('sample') | |
export function sample(v: string) { | |
return sampleA(v + '_sample'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import proxyquire from 'proxyquire'; | |
import { expect } from 'chai'; | |
// import sinon from 'sinon'; | |
// Important to prevent proxyquire from going into the original dependency. | |
/** | |
* Sample.xspec | |
* | |
* #noCallThru | |
* log => sample | |
* | |
* #callThru | |
* log => sampleB <--- notice that will call through, sample-b which is a dependency from sample-a is loaded. | |
* log => sample | |
* | |
*/ | |
describe('Sample.xspec', () => { | |
let sample; | |
step('noCallThru', () => { | |
proxyquire.noCallThru(); | |
sample = proxyquire('./sample', { | |
'./sample-a': { | |
sampleA: (v) => { | |
return v + '_____mockSampleA'; | |
}, | |
}, | |
}).sample; | |
expect(sample('a')).to.be.equal('a_sample_____mockSampleA'); | |
}); | |
step('callThru', () => { | |
proxyquire.callThru(); | |
sample = proxyquire('./sample', { | |
'./sample-a': { | |
sampleA: (v) => { | |
return v + '_____mockSampleA'; | |
}, | |
}, | |
}).sample; | |
// console.log({ sample }); | |
// console.log('#1', sample.sampleA('a')) | |
// console.log('#1', sample.sampleA2('a')) | |
// expect(sample.sampleA('a')).to.be.undefined; | |
expect(sample('a')).to.be.equal('a_sample_____mockSampleA'); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"compilerOptions": { | |
"baseUrl": ".", | |
"sourceMap": true, | |
"declaration": false, | |
"module": "CommonJS", | |
"moduleResolution": "node16", | |
"emitDecoratorMetadata": true, | |
"experimentalDecorators": true, | |
"target": "ES2020", | |
"allowJs": true, | |
"removeComments": false, | |
"typeRoots": ["*.d.ts", "**/*.d.ts", "../node_modules/@types"], | |
"lib": ["dom", "ES2020"], | |
"esModuleInterop": true | |
}, | |
"include": ["../src/**/*", "./**/*.ts"], | |
"exclude": ["../node_modules"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment