GithubHelp home page GithubHelp logo

cmorten / superoak Goto Github PK

View Code? Open in Web Editor NEW
120.0 120.0 8.0 560 KB

HTTP assertions for Oak made easy via SuperDeno. ๐Ÿฟ ๐Ÿฆ•

Home Page: https://cmorten.github.io/superoak/

License: MIT License

Makefile 1.49% TypeScript 98.51%
assertions deno deno-doc http-assertions http-testing oak superdeno superoak supertest testing typescript

superoak's People

Contributors

asos-craigmorten avatar cmorten avatar ding-fan avatar seanaye avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

superoak's Issues

Does not send correctly an application/x-www-form-urlencoded

Issue

Setup:

  • Deno Version: 1.17.0 (release, aarch64-apple-darwin)
  • v8 Version: 9.7.106.15
  • Typescript Version: 4.5.2
  • SuperOak Version: 4.5.0

Details

When sending a form like:

superoak(app)
  .post("/")
  .type("form")
  .field("hello", "world")
  .field("foo", "bar")

Instead of receiving {hello: "world", foo: "bar"} I'm receiving the following:

{
  "------9219543305020510994576475827\r\nContent-Disposition: form-data; name": '"hello"\r\n\r\nworld\r\n------9219543305020510994576475827\r\nContent-Disposition: form-data; name="foo"\r\n\r\n...'
}

I'm going to open a PR with a failing test to validate this

ReferenceError: Access to "location", run again with --location <href>

Issue

Setup:

  • Deno Version: 1.7.2 (release, x86_64-pc-windows-msvc)
  • v8 Version: 8.9.255.3
  • Typescript Version: 4.1.3
  • SuperOak Version: 3.0.1

When running the tests with deno test --allow-net app.test.ts I get the above error message.

Details

Following the example I set up the tests and ran deno test --allow-net app.test.ts but this error occurs.

ฮป deno test --allow-net app.test.ts 
running 2 tests
test GET "/" route ... Listening on: http://127.0.0.1:30162
FAILED (9ms)
test GET "/user/:id" ... Listening on: http://127.0.0.1:21887
FAILED (4ms)

failures:

GET "/" route
ReferenceError: Access to "location", run again with --location <href>.
    at get (deno:op_crates/web/12_location.js:376:17)
    at Function.request.getXHR (https://dev.jspm.io/npm:[email protected]/lib/client.dew.js:91:39)
    at Test.Request._end (https://dev.jspm.io/npm:[email protected]/lib/client.dew.js:753:24)
    at Test.Request.end (https://dev.jspm.io/npm:[email protected]/lib/client.dew.js:736:10)
    at Test.end (test.ts:443:9)
    at https://dev.jspm.io/npm:[email protected]/lib/request-base.dew.js:291:14
    at new Promise (<anonymous>)
    at Test.RequestBase.then (https://dev.jspm.io/npm:[email protected]/lib/request-base.dew.js:273:33)

GET "/user/:id"
ReferenceError: Access to "location", run again with --location <href>.
    at get (deno:op_crates/web/12_location.js:376:17)
    at Function.request.getXHR (https://dev.jspm.io/npm:[email protected]/lib/client.dew.js:91:39)
    at Test.Request._end (https://dev.jspm.io/npm:[email protected]/lib/client.dew.js:753:24)
    at Test.Request.end (https://dev.jspm.io/npm:[email protected]/lib/client.dew.js:736:10)
    at Test.end (test.ts:443:9)
    at https://dev.jspm.io/npm:[email protected]/lib/request-base.dew.js:291:14
    at new Promise (<anonymous>)
    at Test.RequestBase.then (https://dev.jspm.io/npm:[email protected]/lib/request-base.dew.js:273:33)

failures:

        GET "/" route
        GET "/user/:id"

test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out (14ms)

Hack to get the tests run again

Passing in any random "valid" URL like deno test --allow-net --location http://anything app.test.ts makes the tests run again.

deno test --allow-net --location http://anything app.test.ts 
running 2 tests
test GET "/" route ... Listening on: http://127.0.0.1:17370
ok (18ms)
test GET "/user/:id" ... Listening on: http://127.0.0.1:26924
ok (7ms)

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (29ms)

It even runs when I just pass in --location http://a. I hope this only has to do with the Deno update to 1.7.2.

Thanks in advance for looking into it. :)

Need a FAQ section in README.md

Issue

Setup:

  • Deno Version: 1.5.4 (bc79d55, release, x86_64-apple-darwin)
  • v8 Version: 8.8.278.2
  • Typescript Version: 4.0.5
  • SuperOak Version: 2.4.1

Details

Use the same superoak instance multiple times in a same test

// ...
Deno.test("app", async () => {
  const testClient = await superoak(app);
  await testClient.get("/").expect("Hello GET!");
  await testClient.post("/").expect("Hello POST!");
});
// ...

leads to vague error message like below:

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 (https://dev.jspm.io/npm:[email protected]/lib/client.dew.js:680:15)
    at XMLHttpRequestSham.xhr.onreadystatechange (https://dev.jspm.io/npm:[email protected]/lib/client.dew.js:783:21)
    at XMLHttpRequestSham.xhrReceive (https://deno.land/x/[email protected]/src/xhrSham.js:105:29)
    at XMLHttpRequestSham.send (https://deno.land/x/[email protected]/src/xhrSham.js:64:12)

I'm not sure is this a bug or not. Maybe add this to a FAQ section in README.md would save people's time. ๐Ÿ™‚

AssertionError: Test case is leaking async ops.

Issue

image

Setup:

deno 1.1.0
v8 8.4.300
typescript 3.9.2
oak version: v5.3.0

Getting the following error (screenshot above)

AssertionError: Test case is leaking async ops.
Before:
  - dispatched: 7
  - completed: 6
After:
  - dispatched: 23
  - completed: 23

Am I meant to chain a .end() to the last request in each test file?

Tests hang when running with --unstable (Oak 7.3, SuperOak 4.2, Deno 1.9.2)

Issue

Hi! When running Deno superoak tests with --unstable flag in recent Deno / Oak -versions, the tests get stuck.

Setup:

  • Deno Version: 1.9.0+ (earlier versions, e.g. 1.8.3, not affected)
  • Oak Version: 7.0.0+ (earlier versions, e.g. v6.5.0, not affected)
  • SuperOak Version: 4.0.0+

Details

Example code:

import { Application } from "https://deno.land/x/[email protected]/mod.ts";
import { superoak } from "https://deno.land/x/[email protected]/mod.ts";

const app = new Application();

const greet = (context) => {
  context.response.body = "Hello world!";
};

app.use(greet);

Deno.test("Response is 'Hello world!'", async () => {
  const response = await superoak(app);
  await response
    .get("/")
    .expect("Hello world!");
});

When running the tests with deno test --allow-net --unstable app.js, the tests get stuck. However, with deno test --allow-net app.js, the tests can be successfully run. This might have something to do with the native HTTP server enabled in oak when using the --unstable-flag, but have not looked into this in detail. Oak can be normally used with the --unstable flag though.

Project does not compile with latest version of oak using Deno 1.3.0

Issue

Project not compiling using oak

Setup:
macOS Big Sur
deno 1.3.0
v8 8.6.334
typescript 3.9.7

  • SuperOak Version: 4.0.0

Details

Not sure exactly why but my project is not compiling with the latest version of superoak and oak

Here is a reproducible case:

deps.ts

export {
  Application,
  Router,
} from "https://deno.land/x/oak/mod.ts";

export { superoak } from "https://deno.land/x/superoak/mod.ts";

app.ts

import { Application, Router } from './deps.ts';

const router = new Router();
router
  .get('/', (context) => {
    context.response.body = 'Hello ๐Ÿ‘‹';
  })

const app = new Application();

export { app };
app.use(router.routes());

await app.listen({ port: 8080 });

I am running

/usr/local/bin/den ./app.ts

I can`t install it

Issue

Setup:

deno 1.0.5
v8 8.4.300
typescript 3.9.2

Details

What i did:
copy paste your demo code on the github

The Error:
error: error sending request for url (https://raw.githubusercontent.com/garronej/minimal_polyfills/2.1.0/Set.ts): error trying to connect: dns error: ่ฏทๆฑ‚็š„ๅ็งฐๆœ‰ๆ•ˆ๏ผŒไฝ†ๆ˜ฏๆ‰พไธๅˆฐ่ฏทๆฑ‚็š„็ฑปๅž‹็š„ๆ•ฐๆฎใ€‚ (os error 11004)

translation: title of the request is valid, but could not find the data of requesting type.(sorry for the bad translation)

Demo Code Throws Errors

Issue

When running the demo script it throws four errors.

Setup:

  • Deno Version: 1.18.2
  • v8 Version: 9.8.177.6
  • Typescript Version: 4.5.2
  • SuperOak Version: 4.6.0

Details

The script was copied from the readme file and named demo.test.ts

$ deno test --allow-net demo.test.ts

error: TS2322 [ERROR]: Type '{}' is not assignable to type 'RouteParams<R>'.
  params: RouteParams<R> = {},
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    at https://deno.land/x/[email protected]/router.ts:223:3

TS2322 [ERROR]: Type '{}' is not assignable to type 'RouteParams<R>'.
  let replace: RouteParams<R> = {};
      ~~~~~~~
    at https://deno.land/x/[email protected]/router.ts:227:7

TS2322 [ERROR]: Type '{}' is not assignable to type 'RouteParams<R>'.
    existingParams: RouteParams<R> = {},
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    at https://deno.land/x/[email protected]/router.ts:301:5

TS2322 [ERROR]: Type '{}' is not assignable to type 'RouteParams<R>'.
    params: RouteParams<R> = {},
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    at https://deno.land/x/[email protected]/router.ts:321:5

Found 4 errors.

Example fails with "Re-exporting a type.."

Issue

I followed the advertized example in creating a file "demo.test.ts" and ran deno test --allow-net demo.test.ts in a fresh directory. Here's the output:

Check file:///.../deno/testing/demo.test.ts
error: TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { AsyncReturnType } from "./AsyncReturnType.ts";
         ~~~~~~~~~~~~~~~
    at https://deno.land/x/[email protected]/tools/typeSafety/index.ts:2:10

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { UnpackPromise } from "./UnpackPromise.ts";
         ~~~~~~~~~~~~~
    at https://deno.land/x/[email protected]/tools/typeSafety/index.ts:8:10

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { UnpackTypeGuard } from "./UnpackTypeGuard.ts";
         ~~~~~~~~~~~~~~~
    at https://deno.land/x/[email protected]/tools/typeSafety/index.ts:9:10

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { Ctx, DoneOrAborted } from "./Ctx.ts";
         ~~~
    at https://deno.land/x/[email protected]/lib/types/interfaces/index.ts:1:10

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { Ctx, DoneOrAborted } from "./Ctx.ts";
              ~~~~~~~~~~~~~
    at https://deno.land/x/[email protected]/lib/types/interfaces/index.ts:1:15

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { CtxLike, VoidCtxLike } from "./CtxLike.ts";
                  ~~~~~~~~~~~
    at https://deno.land/x/[email protected]/lib/types/interfaces/index.ts:2:19

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { Evt } from "./Evt.ts";
         ~~~
    at https://deno.land/x/[email protected]/lib/types/interfaces/index.ts:3:10

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { NonPostableEvt } from "./NonPostableEvt.ts";
         ~~~~~~~~~~~~~~
    at https://deno.land/x/[email protected]/lib/types/interfaces/index.ts:4:10

TS1205 [ERROR]: Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.
export { Postable } from "./Postable.ts";
...

Setup:

  • Deno Version: 1.16.1
  • v8 Version: 9.7.106.2
  • Typescript Version: 4.4.2
  • SuperOak Version: 2.1.0

AssertionError: Test case is leaking async ops.

Issue

Setup:

  • Deno Version: 1.4.4
  • v8 Version: 8.7.75
  • Typescript Version: 4.0.3
  • SuperOak Version: 2.3.1

I am trying to run a simple test in my app, but it constantly fails because of leaking async ops, even though there's nothing running.

Details

import { Application} from "https://deno.land/x/[email protected]/mod.ts";
import { superoak } from "https://deno.land/x/[email protected]/mod.ts";

const app = new Application();
app.use((ctx) => {
  ctx.response.body = "Hello world!";
});

Deno.test("Example", async () => {
  const request = await superoak(app);
});

To run: deno test --allow-net <that code>.ts

Error log:

running 1 tests
test test ... FAILED (8ms)

failures:

test
AssertionError: Test case is leaking async ops.
Before:
  - dispatched: 0
  - completed: 0
After:
  - dispatched: 2
  - completed: 1

Make sure to await all promises returned from Deno APIs before
finishing test case.
    at assert (deno:cli/rt/06_util.js:33:13)
    at asyncOpSanitizer (deno:cli/rt/40_testing.js:44:7)
    at async Object.resourceSanitizer [as fn] (deno:cli/rt/40_testing.js:68:7)
    at async TestRunner.[Symbol.asyncIterator] (deno:cli/rt/40_testing.js:240:13)
    at async Object.runTests (deno:cli/rt/40_testing.js:317:22)

failures:

        test

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out (8ms)

Document how to send request body, headers, etc

Issue

I cannot find any documented way of sending a post request with a body or setting custom headers on the request. According to the docs Test.post(url:string) there is only a url accepted. How should I set headers and post body on request?

Setup:

  • deno 1.11.5 (release, aarch64-apple-darwin)
  • v8 9.1.269.35
  • typescript 4.3.2
  • SuperOak Version: 4.3.0

Details

Please replace this quote block with the details of the feature / bug you wish
to be addressed. If it is a bug please do your best to add steps to reproduce.

Testing error case fails

Issue

Trying to test that an endpoint returns an error status code (like 400) fails.

Details

https://github.com/twhitbeck/superoak-testcase

  • run the server
  • curl localhost:8080/create -H "Content-Type: application/json" -d "{\"url\":\"foo\"}" -v
  • note response status code is 400

I'm trying to create a test case that this endpoint will respond with 400.

  • deno test --allow-net
[uncaught oak error]: BadRequestError - Invalid url

request: { url: "http://127.0.0.1:19542/create", method: "POST", hasBody: true }
response: { status: 404, type: undefined, hasBody: false, writable: true }

    at createHttpError (https://deno.land/x/[email protected]/httpError.ts:128:10)
    at Context.throw (https://deno.land/x/[email protected]/context.ts:177:17)
    at file:///home/twhitbeck/src/local/superoak-testcase/server.ts:18:18
    at async dispatch (https://deno.land/x/[email protected]/middleware.ts:41:7)
    at async dispatch (https://deno.land/x/[email protected]/middleware.ts:41:7)
    at async dispatch (https://deno.land/x/[email protected]/middleware.ts:41:7)
    at async Application.#handleRequest (https://deno.land/x/[email protected]/application.ts:378:9)
test it should respond with 400 when the url is marlformed ... FAILED (18ms)

failures:

it should respond with 400 when the url is marlformed
AssertionError: Test case is leaking async ops.
Before:
  - dispatched: 2
  - completed: 2
After:
  - dispatched: 13
  - completed: 12

Make sure to await all promises returned from Deno APIs before
finishing test case.
    at assert (deno:runtime/js/06_util.js:42:13)
    at asyncOpSanitizer (deno:runtime/js/40_testing.js:48:7)
    at async resourceSanitizer (deno:runtime/js/40_testing.js:72:7)
    at async exitSanitizer (deno:runtime/js/40_testing.js:99:9)
    at async runTest (deno:runtime/js/40_testing.js:215:7)
    at async Object.runTests (deno:runtime/js/40_testing.js:283:22)
    at async file:///home/twhitbeck/src/local/superoak-testcase/c7629de1-c472-4780-a46d-d82b87eba195$deno$test.js:4:1

failures:

        it should respond with 400 when the url is marlformed

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out (90208ms)

error: Test failed

callback function parameters are displaced.

Issue

Setup:

  • Deno Version: 1.5.3
  • v8 Version: 8.7.220.3
  • Typescript Version: 4.0.5
  • SuperOak Version: 2.3.1

Callback function parameters are displaced.

Details

I want to use callback function like below but the test fails because res is undefined.

Deno.test("test", async () => {
  const req = await superoak(app);
  await req.post("/").expect((err, res) => {
    expect(res.body.message).toEqual("Hello Deno!");
  });
});

Callback described as CallbackHandler(err: any, res: IResponse): void in typescript autocomplete, but actually the first argument contains response value so it should be CallbackHandler(res: IResponse, err: any): void.

How to test cookie based sessions?

How to test cookie based sessions?

Setup:

Details

Hello, I'm trying to figure out how to use superoak to test multiple routes that would sign-up and sign-in a user via session cookie. Is there any way to read headers from response and pass them to a request?

error: TS2345 [ERROR]: Argument of type 'Application<Record<string, any>>' is not assignable to parameter of type 'string | Application<Record<string, any>>'.

Issue

Setup:

deno 1.0.3
v8 8.4.300
typescript 3.9.2

const router = new Router();
const app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());

const response: SuperDeno = await superoak(app);

Running this code gives me:

Details

error: TS2345 [ERROR]: Argument of type 'Application<Record<string, any>>' is not assignable to parameter of type 'string | Application<Record<string, any>>'.
  Type 'import("https://deno.land/x/oak/application").Application<Record<string, any>>' is not assignable to type 'import("https://deno.land/x/[email protected]/application").Application<Record<string, any>>'.
    Property '#middleware' in type 'Application' refers to a different member that cannot be accessed from within type 'Application'.
    const response: SuperDeno = await superoak(app);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.