cmorten / superoak

HTTP assertions for Oak made easy via SuperDeno. 🐿 🦕
https://cmorten.github.io/superoak/
MIT License
121 stars 8 forks source link

[#13] Update README.md add FAQs section #14

Closed Ding-Fan closed 3 years ago

Ding-Fan commented 3 years ago

Issue

13

Details

Add FAQs section to README.md

CheckList

cmorten commented 3 years ago

Hey @Ding-Fan, following on from your comment here. That second note is meant to be your issue, just very poorly explained 😅

What might work well is if we combine your FAQs suggestions with the existing Notes section (can rename / replace it all with FAQs), see an example suggestion below:

## FAQs

### `Property 'get' does not exist on type 'Promise<SuperDeno>'` error

Unlike [SuperDeno](https://github.com/asos-craigmorten/superdeno), `superoak()` returns a promise which will need to be awaited before you can call any method such as `.get("/")`.

\`\`\`ts
// ✅ works
Deno.test("it will allow you to make assertions if you await it", async () => {
  const request = await superoak(app);
  await request.get("/").expect(200).expect("Hello Deno!");
});

// ❌ won't work
Deno.test("it will allow you to make assertions if you await it", async () => {
  const request = superoak(app);
  await request.get("/").expect(200).expect("Hello Deno!"); // Boom 💥 `Property 'get' does not exist on type 'Promise<SuperDeno>'`
});
\`\`\`

### `Request has been terminated` error

Unlike [SuperDeno](https://github.com/asos-craigmorten/superdeno), you cannot re-use SuperOak instances. If you try you will encounter an error similar to below:

\`\`\`console
Error: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
    at Test.Request.crossDomainError
    at XMLHttpRequestSham.xhr.onreadystatechange
    ...
\`\`\`

This is because SuperOak instances automatically close the underlying Oak server once the assertion chain has completed.

Instead you should make all of your assertions on a single SuperOak instance, or create a new SuperOak instance for subsequent assertions like below:

\`\`\`ts
// ✅ works
Deno.test("it will allow your to make multiple assertions on one SuperOak instance", async () => {
  const request = await superoak(app);
  await request.get("/").expect(200).expect("Hello Deno!");
});

// ✅ works
Deno.test("it will allow your to re-use the Application for another SuperOak instance", async () => {
  const request1 = await superoak(app);
  await request1.get("/").expect(200);

  const request2 = await superoak(app);
  await request2.get("/").expect("Hello Deno!");
});

// ❌ won't work
Deno.test("it will through an error if try to re-use a SuperOak instance", async () => {
  const request = await superoak(app);
  await request.get("/").expect(200);
  await request.get("/").expect("Hello Deno!"); // Boom 💥 `Error: Request has been terminated`
});
\`\`\`

(excuse the backslashes for the code sections, can't have a code block inside a code block 😋)

Though you've provided several nice examples, I don't think it's transparent what the works and won't work examples are showing (as it would require new users to dive into an issue for context which is another click etc.).

Even with the context, I think we can use the test names to provide context why an example works vs doesn't work (sim. to the exisitng notes).

Ding-Fan commented 3 years ago

Hi @cmorten , your example looks good 👍 I'll make a new commit with your example to replace the existing Notes section.