generated from json-schema-org/repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve testing by checking a file is created wit hthe correct value …
…as opposed to checking a function waw called
- Loading branch information
1 parent
3dd0683
commit 8f8b2cb
Showing
3 changed files
with
63 additions
and
30 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,9 +1,8 @@ | ||
/* eslint-disable no-undef */ | ||
import { server } from './mocks/server'; | ||
import { vol } from 'memfs'; | ||
beforeAll(() => server.listen()); | ||
beforeEach(() => { | ||
vol.reset(); | ||
|
||
beforeAll(() => { | ||
server.listen(); | ||
}); | ||
afterEach(() => server.resetHandlers()); | ||
afterAll(() => server.close()); | ||
afterAll(() => server.close()); |
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,15 +1,13 @@ | ||
import { http, HttpResponse } from 'msw'; | ||
|
||
export const releaseDateHandler = | ||
http.get('https://api.github.com/*', () => { | ||
return HttpResponse.json({ | ||
'repository': 'octocat/hello-world', | ||
'names': 'python, json, json-schema, docker, postgresql, frontend, backend, fastapi, traefik, letsencrypt, swagger, jwt, openapi, chakra-ui, react, tanstack-query, tanstack-router, typescript, sqlmodel', | ||
'date_first_commit': '2019-02-09T15:42:36Z', | ||
'created_at': '2019-02-23T15:08:34Z', | ||
'date_first_release': '2019-03-11T09:49:01Z', | ||
}); | ||
} | ||
); | ||
export const releaseDateHandler = http.get('https://api.github.com/*', () => { | ||
return HttpResponse.json({ | ||
repository: 'octocat/hello-world', | ||
names: 'python, json, json-schema, docker, postgresql', | ||
date_first_commit: '2019-02-09T15:42:36Z', | ||
created_at: '2019-02-23T15:08:34Z', | ||
date_first_release: '2019-03-11T09:49:01Z', | ||
}); | ||
}); | ||
|
||
export const handlers = [releaseDateHandler]; | ||
export const handlers = [releaseDateHandler]; |
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,31 +1,67 @@ | ||
/* eslint-disable no-undef */ | ||
import { tmpdir } from 'node:os'; | ||
import { sep } from 'node:path'; | ||
import fs from 'fs'; | ||
|
||
import { fetchRepoCreationDate } from './main.js'; | ||
import { getInput } from './setup.js'; | ||
import { Octokit } from 'octokit'; | ||
import { DataRecorder } from './dataRecorder.js'; | ||
import fs from 'fs'; | ||
import { describe, it, expect, afterEach, afterAll } from '@jest/globals'; | ||
|
||
const { token } = getInput(); | ||
const octokit = new Octokit({ auth: token }); | ||
const tmpDir = tmpdir(); | ||
const tmpDirForTests = fs.mkdtempSync(`${tmpDir}${sep}`); | ||
|
||
describe('Basic tests', () => { | ||
afterEach((done) => { | ||
fs.rm(tmpDirForTests, { recursive: true, force: true }, (err) => { | ||
if (err) { | ||
console.log(err); | ||
done(err); | ||
} else { | ||
console.debug('temp directory cleared'); | ||
fs.mkdir(tmpDirForTests, {}, (err) => { | ||
if (err) { | ||
console.log(err); | ||
done(err); | ||
} else { | ||
done(); | ||
} | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
describe('Skretch Objective', () => { | ||
let data; | ||
it('receives a mocked response from the GitHub API', async () => { | ||
afterAll((done) => { | ||
fs.rm(tmpDirForTests, { recursive: true, force: true }, (err) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
console.log(`${tmpDirForTests} is deleted!`); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Simple use of Octokit calls the GitHub API', async () => { | ||
let data; | ||
data = await fetchRepoCreationDate(octokit, 'octocat', 'hello-world'); | ||
expect(data).toEqual(1550934514000); | ||
|
||
|
||
}); | ||
|
||
it('writes JSON data to csv file', async() => { | ||
it('DataRecorder writes JSON data to csv file', async () => { | ||
const response = { | ||
'date_first_commit': 1550934514000, | ||
date_first_commit: 1550934514000, | ||
}; | ||
const spy = jest.spyOn(DataRecorder.prototype, 'appendToCSV').mockImplementation(() => fs.appendFileSync('test.csv', '1550934514000', 'utf8')); | ||
const dataRecorder = new DataRecorder('test.csv'); | ||
const fileName = `${tmpDirForTests}${sep}test.csv`; | ||
|
||
const dataRecorder = new DataRecorder(fileName); | ||
dataRecorder.appendToCSV([response.date_first_commit]); | ||
const fileContent = fs.readFileSync(fileName, 'utf8'); | ||
|
||
expect(spy).toHaveBeenCalled(); | ||
expect(spy).toHaveBeenCalledWith([data]); | ||
expect(fileContent).toEqual( | ||
expect.stringContaining(response.date_first_commit.toString()), | ||
); | ||
}); | ||
}); |