Giter VIP home page Giter VIP logo

now-we-test's Introduction

Now we test

Build Status coverage oclif npm version

Temporary lib to help test Now.sh lambdas written in Node.js. It was created with the purpose to eventually be deprecated when now dev command is released.

Purpose

Help unit test and locally serve lambdas written to deploy using Now.

Installation

yarn add now-we-test --dev

Usage

For unit tests

In your unit tests, you can use the simulator helper to simulate a lambda execution environment without needing a running server:

import { simulator } from 'now-we-test'
import func from '../your-lambda-implementation'

const app = simulator(func)

describe('func', () => {
  it('should do something', async () => {
    const result = await app.get('/')

    expect(result.text).toBe('[the value returned by the lambda]')
  })
})

Signature:

simulator(
  fn: Function,
  [builder="@now/node"]: String
)

Alternatively to the default @now/node building evironment, you can also simulate a now-micro passing it as second parameter.

The result of the invokation of simulator helper is a SuperTest instance, so please refer to that documentation for details on possible assertions and usage.

For locally serving lambdas

Now we test provides a simple CLI to serve your lambdas locally - useful for testing integration with other tools.

Keep in mind there is only so much it can do in terms of simulation, so you should never completelly trust what works locally would necessarily work on Now.sh environment.

Commands

now-we-test serve

Serves the lambdas locally

USAGE
  $ now-we-test serve [PATH]

ARGUMENTS
  PATH  [default: .] The path to the directory where now.json stands

OPTIONS
  -f, --format=format  [default: combined] logger format (morgan compliant)
  -p, --port=port      [default: 3000] port to use

See code: lib/commands/serve.js

Only @now/node, @now/static, and now-micro builders are available so far.

now-we-test help [COMMAND]

display help for a now-we-test command

USAGE
  $ now-we-test help [COMMAND]

ARGUMENTS
  COMMAND  command to show help for

OPTIONS
  --all  see all commands in CLI

See code: @oclif/plugin-help

now-we-test's People

Contributors

agstover avatar lucasconstantino 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

Watchers

 avatar  avatar  avatar

now-we-test's Issues

Query strings failing on routes

Routes using query string might be rebuild to wrong paths.

i.e.:

For routes:

  "routes": [
    {
      "src": "/(?<id>.*)",
      "dest": "/src/hooks/$id.js",
      "headers": { "Access-Control-Allow-Origin": "*" }
    }
  ]

And request

/foo?bar=tor`

The resolved path is currently:

/src/hooks/foo?bar=tor.js

Add Regex Notes

Would totally understand if you don't want to bother with this, but for those of us not well versed in Regex would it be possible to add //comments in the code explaining what's happening here. Or maybe just comments here if you don't want them in the code. Thank you!

Found in lib/commands/serve.js

req.url.match(new RegExp(^${src.replace(/(?<[a-z]+>/, '(')}$))

dest.replace(new RegExp('\\$' + id), $+{${id}})

TypeScript support

Hey, @Meesayen, perhaps we can work on a pull-request to add TypeScript support based on your fork? Thanks for the work, by the way ;)

How I can test a POST method

I was looking at the code and I can't saw nowhere the method send of supertest lib, I guess without that it is not possible to test the POST method right? Can you provide some examples how do you test the POST methods?

Thanks for advance.

Support for node@canary

It would be great to support the @now/node@canary builder. For the purpose of this simulator that should be easy since it will simply do the same thing as @now/node here I think.

I can make a PR if you're open to this idea.

Expected lambda file not found

I cannot reach any routes deeper than /.

Here's my now.json routes section:

"routes": [
  { "src": "/", "dest": "/services/index.js" },
  { "src": "/locations", "methods": ["GET"], "dest": "/services/locations/read.js" },
  { "src": "/users", "methods": ["GET"], "dest": "/services/users/read.js" },
  { "src": "/users/(?<id>[^/]*)", "methods": ["GET"], "dest": "/services/users/read.js?id=$id" },
  { "src": "/users", "methods": ["POST"], "dest": "/services/users/create.js" }
]

If I try to reach /locations, I get the error Expected lambda file not found. Deployed on Now 2.0, this works, so there is a problem with the now-we-test server reaching the .js files specified in the routes.

Routes.find() produces wrong route.

Here is a JSFiddle of the event.

https://jsfiddle.net/5n0whef7/1/

Essentially in the serve.js file, it's looping through my routes, finds a matching route, and then returns the route after the matching route as the value.

If the req.url is "/api/auth", then the right route is returned. If the req.url is "/api/auth/signin", then the .find() function returns the wrong value.

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.