Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Feature: Compatibility with 'app-root-path'

Aperta
#286 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
javascript, node.js
Ambito
testing-qa

Direzione di ricerca

Start by tracing testdouble's td.replace operation and its module-loading entry points, then compare that behavior with app-root-path's require function and absolute-path resolution. Done means app-root-path-style root resolution works with td.replace while preserving the mocked and passthrough behavior shown in the examples.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

When it comes to large codebases, it can be massively beneficial to grab libraries from the root resolution rather than relative path resolution ../../

There is an excellent library to assist with this for NodeJs but its not compatible with testdouble's .replace operation

https://www.npmjs.com/package/app-root-path

Would be great to see this compatibility so assist with a consistent resolution approach through projects.

As an example:
users.js

// NPM Requires
const express = require('express')

// Library Requires
const users = require('./users')
// const users = reqlib('routes/example/users')

// Module Variables

module.exports = function (app) {
  var route = express.Router()

  // Mount route as "/users"
  app.use('/users', route)

  // Add a route that allows us to get a user by their username
  route.get('/:username', function (req, res) {
    var user = users.getByUsername(req.params.username)
console.log(user)
    if (!user) {
      res.status(404).json({
        status: 'not ok',
        data: null
      })
    } else {
      res.json({
        status: 'ok',
        data: user
      })
    }
  })
}

users.spec.js

// NPM Requires
const supertest = require('supertest')
const express = require('express')
const td = require('testdouble')

// Library Requires
const chai = reqlib('helpers/chai-setup')

// Module Variables

describe('GET /ping', function() {
  const interaction = ['Mocked', 'Passthrough']
  const isMockedLoop = [true, false]

  isMockedLoop.forEach(function(isMocked) {
    let app, request, route, users, usersTD

    beforeEach(function() {
      // Create an express application object
      app = express()

      // Get our router module, with a stubbed out users dependency
      // we stub this out so we can control the results returned by
      // the users module to ensure we execute all paths in our code
      if (isMocked) {
        users = td.replace('./users')
        usersTD = users.getByUsername

        // Setup generic returns for each td function
        td.when(usersTD(td.matchers.anything())).thenReturn(null)
      }

      route = require('./user-route')
      // route = reqlib('routes/example/user-route')

      // Bind our application to
      route(app)

      // Get a supertest instance so we can make requests
      request = supertest(app)
    })

    it(interaction[isMockedLoop.indexOf(isMocked)] + ': should respond with a 404 and a null', function() {
      if (isMocked) {
        // td.when(usersTD('nodejs')).thenReturn({
        //   username: 'test'
        // })
      } else {
        // Skip test until the mocked function has been coded
        this.skip()
      }

      return request
        .get('/users/nodejs')
        .expect(404)
        .then((res) => {
          td.verify(users.getByUsername(td.matchers.anything()))
        })
    })

    it(interaction[isMockedLoop.indexOf(isMocked)] + ': should respond with 200 and a user object', function() {
      console.log(isMocked)
      if (isMocked) {
        td.when(usersTD('nodejs')).thenReturn({
          username: 'test'
        })
      } else {
        // Skip test until the mocked function has been coded
        this.skip()
      }

      return request
        .get('/users/nodejs')
        .expect(200)
        .then((res) => {
          td.verify(users.getByUsername(td.matchers.anything()))
        })
    })

    afterEach(function() {
      if (isMocked) {
        td.reset()
      }
    })
  })
})

app-root-path is enabled by including a global in the root of the call chain
global.reqlib = require('app-root-path').require

when trying to use reqlib to load require td isn't called, even is absolute paths are used

Lingua principale
JavaScript
Stelle
1.4k
Fork
140
Merge medio
5h 55m
PR unite (30g)
4

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di testdouble/testdouble.js

Tutte le issue di testdouble/testdouble.js

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.