Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bigbizze committed Jan 9, 2024
1 parent d26edad commit d272492
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 58 deletions.
137 changes: 87 additions & 50 deletions src/app/index.test.js
Expand Up @@ -7,15 +7,22 @@ import bundlewatchApi from '.'

describe(`bundlewatch Node API`, () => {
it('Works with basic options', async () => {
const result = await bundlewatchApi({
files: [
{
path: './__testdata__/*.jpg',
maxSize: '100kB',
},
],
defaultCompression: 'none',
})
const result = await bundlewatchApi(
{
files: [
{
path: './__testdata__/*.jpg',
maxSize: '100kB',
},
],
defaultCompression: 'none',
},
{
error: () => {},
pass: () => {},
fail: () => {},
},
)

// TODO: assert logger.warn called

Expand All @@ -24,14 +31,21 @@ describe(`bundlewatch Node API`, () => {
})

it(`Works when files dont exist, shows warning`, async () => {
const result = await bundlewatchApi({
files: [
{
path: './__testdata__/test-file-doesnt-exist.jpg',
maxSize: '100kB',
},
],
})
const result = await bundlewatchApi(
{
files: [
{
path: './__testdata__/test-file-doesnt-exist.jpg',
maxSize: '100kB',
},
],
},
{
error: () => {},
pass: () => {},
fail: () => {},
},
)

delete result.url
expect(result).toMatchSnapshot()
Expand Down Expand Up @@ -64,22 +78,30 @@ describe(`bundlewatch Node API`, () => {

// TODO: assert save was called

const result = await bundlewatchApi({
files: [
{
path: './__testdata__/*.jpg',
maxSize: '1MB',
const result = await bundlewatchApi(
{
files: [
{
path: './__testdata__/*.jpg',
maxSize: '1MB',
},
],
ci: {
githubAccessToken: MOCK_AUTH_TOKEN,
repoOwner: MOCK_REPO.owner,
repoName: MOCK_REPO.name,
repoCurrentBranch: MOCK_REPO.currentBranch,
repoBranchBase: MOCK_REPO.branchBase,
commitSha: MOCK_REPO.branchBase,
},
],
ci: {
githubAccessToken: MOCK_AUTH_TOKEN,
repoOwner: MOCK_REPO.owner,
repoName: MOCK_REPO.name,
repoCurrentBranch: MOCK_REPO.currentBranch,
repoBranchBase: MOCK_REPO.branchBase,
commitSha: MOCK_REPO.branchBase,
},
})

{
error: () => {},
pass: () => {},
fail: () => {},
},
)

delete result.url
expect(result).toMatchSnapshot()
Expand All @@ -88,32 +110,47 @@ describe(`bundlewatch Node API`, () => {
it('Throws validations error when using brotli compression without the package', async () => {
let error
try {
await bundlewatchApi({
files: [
{
path: './__testdata__/*.jpg',
maxSize: '100kB',
},
],
defaultCompression: 'brotli',
})
await bundlewatchApi(
{
files: [
{
path: './__testdata__/*.jpg',
maxSize: '100kB',
},
],
defaultCompression: 'brotli',
},

{
error: () => {},
pass: () => {},
fail: () => {},
},
)
} catch (e) {
error = e
}
expect(error).toMatchSnapshot()
})

it('Normalizes hash when given a normalizeFilenames option', async () => {
const result = await bundlewatchApi({
files: [
{
path: './__testdata__/*.js',
maxSize: '100kB',
},
],
defaultCompression: 'none',
normalizeFilenames: '^.+?(\\.\\w+)\\.(?:js|css)$',
})
const result = await bundlewatchApi(
{
files: [
{
path: './__testdata__/*.js',
maxSize: '100kB',
},
],
defaultCompression: 'none',
normalizeFilenames: '^.+?(\\.\\w+)\\.(?:js|css)$',
},
{
error: () => {},
pass: () => {},
fail: () => {},
},
)

delete result.url
expect(result.fullResults[0].filePath).toMatchInlineSnapshot(
Expand Down
15 changes: 7 additions & 8 deletions src/bin/index.js
Expand Up @@ -107,15 +107,14 @@ const mainSafe = async () => {
try {
const errorCode = await Promise.race([
main(githubService),
(async () => {
await new Promise((resolve) => {
if (config.maxTimeout != null) {
setTimeout(resolve, config.maxTimeout)
}
// hang forever if maxTimeout is set to null
})
new Promise((resolve) => {
if (config.maxTimeout != null) {
setTimeout(resolve, config.maxTimeout)
}
// hang forever if maxTimeout is set to null
}).then(() => {
throw new Error('Max timeout exceeded')
})(),
}),
])
return errorCode
} catch (error) {
Expand Down

0 comments on commit d272492

Please sign in to comment.