generated from ryansonshine/typescript-npm-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for the initial module code
- Loading branch information
Showing
1 changed file
with
28 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,33 @@ | ||
import { myPackage } from '../src'; | ||
import { logger } from '../src/index'; | ||
|
||
describe('index', () => { | ||
describe('myPackage', () => { | ||
it('should return a string containing the message', () => { | ||
const message = 'Hello'; | ||
describe('Logger', () => { | ||
let consoleLogSpy: jest.SpyInstance; | ||
|
||
const result = myPackage(message); | ||
beforeEach(() => { | ||
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleLogSpy.mockRestore(); | ||
}); | ||
|
||
it("should log a message when NODE_ENV is not 'production'", () => { | ||
process.env.NODE_ENV = 'development'; | ||
const message = 'Test log message'; | ||
|
||
logger.log(message); | ||
|
||
expect(consoleLogSpy).toHaveBeenCalledWith( | ||
expect.stringMatching(/\[LOG\] .*: Test log message/) | ||
); | ||
}); | ||
|
||
it("should not log a message when NODE_ENV is 'production'", () => { | ||
process.env.NODE_ENV = 'production'; | ||
const message = 'Test log message'; | ||
|
||
logger.log(message); | ||
|
||
expect(result).toMatch(message); | ||
}); | ||
expect(consoleLogSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); |