Skip to content

Commit

Permalink
test: show how to propperly mock the github api
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasholzer committed Feb 4, 2022
1 parent 72425d0 commit aee0ff5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/commands/sites/sites-create-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const fetchTemplates = async (token) => {
* @param {import('commander').OptionValues} options
* @param {import('../base-command').BaseCommand} command
*/
const sitesCreate = async (options, command) => {
const sitesCreateTemplate = async (options, command) => {
const { api } = command.netlify

await command.authenticate()
Expand Down Expand Up @@ -224,6 +224,6 @@ Create a site from a starter template.`,
.option('-a, --account-slug [slug]', 'account slug to create the site under')
.option('-c, --with-ci', 'initialize CI hooks during site creation')
.addHelpText('after', `(Beta) Create a site from starter template.`)
.action(sitesCreate)
.action(sitesCreateTemplate)

module.exports = { createSitesFromTemplateCommand }
module.exports = { createSitesFromTemplateCommand, fetchTemplates }
57 changes: 40 additions & 17 deletions tests/integration/140.command.sites.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
const process = require('process')

const test = require('ava')
const execa = require('execa')
const stripAnsi = require('strip-ansi')
const sinon = require('sinon')
// const stripAnsi = require('strip-ansi')

// Important to do the mocks before the code that uses it is required
// in this case the mocks have to be done before the createSitesFromTemplateCommand
// is required!
/* eslint-disable import/order */
const github = require('../../src/utils/init/config-github')
// mock the getGithubToken method with a fake token
sinon.stub(github, 'getGitHubToken').callsFake(() => 'my-token')

/* eslint-enable import/order */

const cliPath = require('./utils/cli-path')
const { CONFIRM, answerWithValue, handleQuestions } = require('./utils/handle-questions')
const { BaseCommand } = require('../../src/commands/base-command')
const { createSitesFromTemplateCommand } = require('../../src/commands/sites/sites-create-template')

// const { CONFIRM, answerWithValue, handleQuestions } = require('./utils/handle-questions')
const { withMockApi } = require('./utils/mock-api')

// TODO: Flaky tests enable once fixed
/**
* As some of the tests are flaky on windows machines I will skip them for now
* @type {import('ava').TestInterface}
*/

test.skip('netlify sites:create-template', async (t) => {
const siteTemplateQuestions = [
{ question: 'Template: (Use arrow keys)', answer: CONFIRM },
{ question: 'Team: (Use arrow keys)', answer: CONFIRM },
{ question: 'Site name (optional)', answer: answerWithValue('test-site-name') },
]
test.skip('netlify sites:create-template', async () => {
// const siteTemplateQuestions = [
// { question: 'Template: (Use arrow keys)', answer: CONFIRM },
// { question: 'Team: (Use arrow keys)', answer: CONFIRM },
// { question: 'Site name (optional)', answer: answerWithValue('test-site-name') },
// ]

const siteInfo = {
admin_url: 'https://app.netlify.com/sites/site-name/overview',
Expand Down Expand Up @@ -48,14 +62,23 @@ test.skip('netlify sites:create-template', async (t) => {
]

await withMockApi(routes, async ({ apiUrl }) => {
const childProcess = execa(cliPath, ['sites:create-template'], {
env: { NETLIFY_API_URL: apiUrl, NETLIFY_AUTH_TOKEN: 'fake-token' },
Object.defineProperty(process, 'env', {
value: {
NETLIFY_API_URL: apiUrl,
NETLIFY_AUTH_TOKEN: 'fake-token',
},
})
handleQuestions(childProcess, siteTemplateQuestions)
const { stdout } = await childProcess

const formattedOutput = JSON.stringify(stripAnsi(stdout)).replace(/\\n/g, '')
const program = new BaseCommand('netlify')
createSitesFromTemplateCommand(program)

await program.parseAsync(['', '', 'sites:create-template'])

// handleQuestions(childProcess, siteTemplateQuestions)
// const { stdout } = await childProcess

// const formattedOutput = JSON.stringify(stripAnsi(stdout)).replace(/\\n/g, '')

t.true(formattedOutput.includes(siteInfo.id))
// t.true(formattedOutput.includes(siteInfo.id))
})
})

0 comments on commit aee0ff5

Please sign in to comment.