-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(tests): adds basic testing support #8
Changes from all commits
3dc501b
cd44e26
a03858a
b3410a2
a0f7482
55651a6
0e73751
9765a68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const fs = jest.genMockFromModule<any>("fs"); | ||
|
||
fs.existsSync = () => true; | ||
|
||
module.exports = fs; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export {run} from '@oclif/command' | ||
export { run } from "@oclif/command"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have prettier set for this? If not we should (typically spaces before curlies and single quotes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, this is a global prettier config I'm using - we don't have one on the project yet. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
afterEach(() => { | ||
jest.resetModules(); | ||
}); | ||
|
||
test("uses default template if dev does not have custom ones", () => { | ||
jest.dontMock("fs"); | ||
|
||
const { | ||
DEFAULT_TEMPLATE_PATH, | ||
getTemplateLocation | ||
} = require("./getTemplateLocation"); | ||
|
||
const templateLocation = getTemplateLocation(); | ||
|
||
expect(templateLocation).toEqual(DEFAULT_TEMPLATE_PATH); | ||
}); | ||
|
||
test("uses developer custom templates if defined", () => { | ||
jest.mock("fs"); | ||
jest.spyOn(process, "cwd").mockImplementation(() => "/custom/path/to"); | ||
|
||
const { getTemplateLocation } = require("./getTemplateLocation"); | ||
|
||
const templateLocation = getTemplateLocation(); | ||
|
||
expect(templateLocation).toEqual("/custom/path/to/_templates"); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎸 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great test 👏 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as fs from "fs"; | ||
import * as path from "path"; | ||
|
||
// The root of our package, so that we can leverage the scripts in the `package.json` | ||
export const rootDirectory = path.join(__dirname, ".."); | ||
|
||
export const DEFAULT_TEMPLATE_PATH = `${rootDirectory}/_templates`; | ||
|
||
// Grab the path of the user's project | ||
export const pathWhereScriptIsRunning = process.cwd(); | ||
|
||
// This is where their templates are (at least we assume so) | ||
const theirTemplatePath = pathWhereScriptIsRunning + "/_templates"; | ||
|
||
const hasTemplates = fs.existsSync(theirTemplatePath); | ||
|
||
export const getTemplateLocation = (): string => | ||
hasTemplates ? theirTemplatePath : DEFAULT_TEMPLATE_PATH; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I love how clean this is! 👍 💯 Nice work! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are your thoughts on mocking the function instead of mocking the whole module?
Would that a better idea? And would it solve the type issue we're currently trying to solve?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately not.
Since the instance of the import in the test is different in the actual app code, the mock doesn't apply to the right one AFAIK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, gotcha. Well, I don't think we should hold this back for that reason.
Ping me when you think this is ready to go and I'll approve!