Skip to content

Instantly share code, notes, and snippets.

@unlocomqx
Last active November 29, 2023 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unlocomqx/aa1bc1aa615773723e452d4c2061ab09 to your computer and use it in GitHub Desktop.
Save unlocomqx/aa1bc1aa615773723e452d4c2061ab09 to your computer and use it in GitHub Desktop.
It's something like this
import { defineConfig } from 'cypress';
import { seed } from './cypress/plugins/seed';
import vitePreprocessor from 'cypress-vite';
export default defineConfig({
video: false,
e2e: {
baseUrl: 'http://localhost:5173',
specPattern: '**/*.cy.ts',
setupNodeEvents(on, config) {
on('task', {
seed: async ({ spec }) => {
return seed(spec);
}
});
on(
'file:preprocessor',
vitePreprocessor({
configFile: './cypress/vite.config.ts',
mode: 'development'
})
);
return config;
}
}
});
// Sample test calling a seed file
describe('Map', () => {
it('display locations', () => {
cy
.task('seed', { spec: 'locations' })
.visit('/map')
.get(`[data-cy=marker]`).should('have.length', 10);
});
});
import { exec } from 'child-process-promise';
import * as fs from 'fs';
import * as path from 'path';
export async function seed(spec: string) {
const file = new URL(`../fixtures/sql/${spec}.sql`, import.meta.url);
if (!fs.existsSync(file.pathname)) {
// file not exists? create a snapshot.
await exportData(file.pathname);
}
if (spec !== 'reset') {
await seed('reset');
}
const command = `psql -U postgres -f ${file.pathname} -d etransport_test`;
return exec(command);
}
async function exportData(pathname: string) {
const dir = path.dirname(pathname);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const command = `pg_dump --dbname=etransport_test --schema=public --data-only --file=${pathname} --column-inserts --username=postgres --host=localhost --port=5432`;
console.log(command);
return exec(command);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment