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

Add an example of working with ava #293

Open
JakubKoralewski opened this issue Feb 17, 2020 · 6 comments
Open

Add an example of working with ava #293

JakubKoralewski opened this issue Feb 17, 2020 · 6 comments
Labels
feature request Feature request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@JakubKoralewski
Copy link

Is your feature request related to a problem? Please describe.
I can't get Ava to work with Typescript and Nuxt. It's very weird, but my project works npm run dev is all fine, but then if I type npm run test with ava not only do I get an error in the test about my types, but also when I run npm run dev from this point on it stops working up until I remove package-lock.json and node_modules!

I believe (before I migrated to 2.11) on 2.0.0 I had a different problem but not this one though.

My ava configuration that I got from somewhere a year ago:

	"ava": {
		"compileEnhancements": false,
		"extensions": [
			"ts"
		],
		"extensions": ["ts"],
		"files": [
			"**/*.test.ts"
		],
		"sources": [
			"src/**/*"
		],
		"require": [
			"esm",
			"ts-node/register"
		],
		"verbose": true
	},

Describe the solution you'd like
An example how to work with testing in Typescript on Nuxt 2.9+

@JakubKoralewski JakubKoralewski added the feature request Feature request label Feb 17, 2020
@kevinmarrec
Copy link
Contributor

@JakubKoralewski Is there any repository with project and basic ava tests you could share ?

@JakubKoralewski
Copy link
Author

JakubKoralewski commented Feb 17, 2020

Sure. This is a site I'm working on. I use ava only here since I couldn't get it to integrate with Nuxt/Vue :/ So it's just ava with Typescript but no Nuxt involved. It uses ava v2. I'm now trying to migrate to v3.

EDIT1: And also this is a test that I can't get to work:

import test from "ava";
import { Nuxt, Builder } from "nuxt";
import { Configuration } from "@nuxt/types";
import { resolve } from "path";
// import vars from "~/assets/scss/_variables.scss";
// import getPort from "get-port";
// import variables from "@/components/Contact/Contact";
// We keep a reference to Nuxt so we can close
// the server at the end of the test

// Init Nuxt.js and start listening on localhost:4000
test.before("Init Nuxt.js", async t => {
	const rootDir = resolve(__dirname, "..");
	let config: Configuration = {};
	try {
		config = require(resolve(rootDir, "nuxt.config.ts")).default;
	} catch (e) {
		console.log("Config error: ", e);
	}
	config.rootDir = rootDir; // project folder
	config.dev = false; // production build
	config.mode = "universal"; // Isomorphic application
	config._typescript = { build: true };
	const nuxt = new Nuxt(config);
	t.context.nuxt = nuxt;
	console.log("Awaiting nuxt ready");
	// await nuxt.ready();
	await new Builder(nuxt).build();
	nuxt.listen(6969, "localhost");
});

// Example of testing only generated html
test("Route / exits and render HTML", async t => {
	const { nuxt } = t.context;
	const context = {};
	const { html }: { html: string } = await nuxt.renderRoute("/", context);
	t.true(html.includes("mireks"));
});

// Example of testing via DOM checking
test("Route / exists and renders HTML with CSS applied", async t => {
	console.log("Rendering window");
	// console.log("nuxt.server: ", nuxt.server);
	const loadedCallback = (x, y, z) => {
		console.log("Loaded callback", x, y, z);
	};

	const window: Window = await nuxt.renderAndGetWindow("/", {}, {loadedCallback, loadingTimeout: 50000});
	const main: HTMLElement = window.document.querySelector(
		"main"
	) as HTMLElement;
	t.not(main, null);
	t.true(main.classList.contains("global-padding"));
	t.is(main.style.paddingLeft, vars.globalPaddingHorizontal);
	t.is(main.style.paddingRight, vars.globalPaddingHorizontal);
});

// Close the Nuxt server
test.after("Closing server", t => {
	t.context.nuxt.close();
});

It doesn't want to wait for Nuxt to finish building the webpage and throws a timeout error :/

@JakubKoralewski
Copy link
Author

I was able to make it work. Changes in this PR.

@vintprox
Copy link

t.context is of unknown type, thus adding property to it creates error Object is of type 'unknown'.
To negotiate it, I had to store nuxt instance in the scope of script.

Also, nuxt contains no typings for Nuxt and Builder, had to shim them to any as people show in #44. Indeed, what a weird sight.

Plus, E2E instructions are not being clear about running Nuxt with its plugins for testing components in isolation.

@vintprox
Copy link

vintprox commented May 17, 2020

Found out that Ava is getting contribution to test.resource that can be used for Nuxt.js server before running any test (all or few, you decide), as well as correctly disposing of it. avajs/ava#1366

/**
 * Proof-of-concept Ava test.resource for setting up Nuxt.js server before running any tests
 * @see https://github.com/avajs/ava/pull/2478
 */

import { resolve } from 'path'
import test from 'ava'
import { Nuxt, Builder } from 'nuxt'
import { Configuration } from '@nuxt/types'

test.resource('Set up Nuxt.js test server', async () => {
  const rootDir = resolve(__dirname, '..')
  let config: Configuration = {}
  try {
    config = require(resolve(rootDir, 'nuxt.config.js'))
  } catch (e) {}
  config.rootDir = rootDir
  config.dev = false
  const nuxt = new Nuxt(config)
  await new Builder(nuxt).build()
  nuxt.listen(3030, 'localhost')

  return async () => {
    nuxt.close()
  }
}

@kevinmarrec
Copy link
Contributor

Anyone wanting to create a PR with minimal ava + TypeScript setup ? :)

@kevinmarrec kevinmarrec added good first issue Good for newcomers help wanted Extra attention is needed labels Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Feature request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants