Skip to content
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

test: verify the generated image links are working #3127

Merged
merged 14 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Integration Test

on:
workflow_dispatch:
schedule:
- cron: '0 3 * * 1' # weekly on Mondays at 03:00

permissions:
contents: read # to fetch code (actions/checkout)

jobs:
integration_test:
name: Integration Test
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- name: Checkout
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
fetch-depth: 0

- name: Install pnpm
uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2 # v4.0.0

- name: Set node version to 22
uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
with:
node-version: 22
cache: 'pnpm'

- name: Install deps
run: pnpm install
env:
CYPRESS_INSTALL_BINARY: 0

- name: Build
run: pnpm run build

- name: Integration Test
run: pnpm run integration-test
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"test": "vitest",
"test:update-snapshots": "vitest run -u",
"coverage": "vitest run --coverage",
"integration-test": "vitest -c vitest.it-config.ts",
"cypress": "cypress",
"docs:test:e2e:ci": "run-s docs:build:ci docs:test:e2e:run",
"docs:test:e2e:run": "run-p --race docs:serve \"cypress run\"",
Expand Down
119 changes: 119 additions & 0 deletions test/integration/modules/image.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Integration tests for the image methods ensuring that the returned urls work.
*/
import https from 'node:https';
import { resolve as urlResolve } from 'node:url';
import { describe, expect, it } from 'vitest';
import { faker } from '../../../src';

/**
* Checks that the given address is a working https address.
*
* An address is considered working, if it:
*
* - is a string
* - starts with https
* - is a proper url
* - returns a http-200 (after redirects)
*
* There is a separate unit test file for checking if the returned URL matches the expectations (domain, parameters, etc.).
*
* @param address The address to check.
*/
async function assertWorkingUrl(address: string): Promise<void> {
expect(address).toBeTypeOf('string');
expect(address).toMatch(/^https:\/\//);
expect(() => new URL(address)).not.toThrow();

await expect(
new Promise((resolve, reject) => {
https
.get(address, ({ statusCode, headers: { location } }) => {
if (statusCode == null) {
reject(new Error(`No StatusCode, expected 200`));
} else if (statusCode === 200) {
resolve(true);
} else if (statusCode >= 300 && statusCode < 400 && location) {
const newAddress = urlResolve(address, location);
assertWorkingUrl(newAddress)
.then(() => resolve(true))
.catch((error: unknown) => {
reject(
new Error(`Failed to resolve redirect to '${location}'`, {
cause: error,
})
);
});
} else {
reject(
new Error(
`Bad StatusCode ${statusCode} expected 200 for '${location}'`
)
);
}
})
.on('error', (error: unknown) => {
reject(new Error(`Failed to get '${address}'`, { cause: error }));
});
})
).resolves.toBe(true);
}

describe('image', () => {
describe('avatar', () => {
it('should return a random avatar url', async () => {
const actual = faker.image.avatar();
await assertWorkingUrl(actual);
});
});

describe('avatarGitHub', () => {
it('should return a random avatar url from GitHub', async () => {
const actual = faker.image.avatarGitHub();
await assertWorkingUrl(actual);
});
});

describe('url', () => {
it('should return a random image url', async () => {
const actual = faker.image.url();
await assertWorkingUrl(actual);
});

it('should return a random image url with a width', async () => {
const actual = faker.image.url({ width: 100 });
await assertWorkingUrl(actual);
});

it('should return a random image url with a height', async () => {
const actual = faker.image.url({ height: 100 });
await assertWorkingUrl(actual);
});

it('should return a random image url with a width and height', async () => {
const actual = faker.image.url({ width: 128, height: 64 });
await assertWorkingUrl(actual);
});
});

describe('urlLoremFlickr', () => {
it('should return a random image url from LoremFlickr', async () => {
const actual = faker.image.urlLoremFlickr();
await assertWorkingUrl(actual);
});
});

describe('urlPicsumPhotos', () => {
it('should return a random image url from PicsumPhotos', async () => {
const actual = faker.image.urlPicsumPhotos();
await assertWorkingUrl(actual);
});
});

describe('urlPlaceholder', () => {
it('should return a random image url from Placeholder', async () => {
const actual = faker.image.urlPlaceholder();
await assertWorkingUrl(actual);
});
});
});
Loading